home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / dispatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  87.7 KB  |  3,488 lines

  1. /* $XConsortium: dispatch.c,v 5.18 90/03/20 11:56:53 keith Exp $ */
  2. /************************************************************
  3. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ********************************************************/
  25.  
  26. #include "X.h"
  27. #define NEED_REPLIES
  28. #define NEED_EVENTS
  29. #include "Xproto.h"
  30. #include "windowstr.h"
  31. #include "fontstruct.h"
  32. #include "dixfontstr.h"
  33. #include "gcstruct.h"
  34. #include "osstruct.h"
  35. #include "selection.h"
  36. #include "colormapst.h"
  37. #include "cursorstr.h"
  38. #include "scrnintstr.h"
  39. #include "opaque.h"
  40. #include "input.h"
  41. #include "servermd.h"
  42. #include "extnsionst.h"
  43.  
  44. extern WindowPtr *WindowTable;
  45. extern xConnSetupPrefix connSetupPrefix;
  46. extern char *ConnectionInfo;
  47. extern void ProcessInputEvents();
  48. extern void ValidateGC();
  49. extern Atom MakeAtom();
  50. extern char *NameForAtom();
  51. extern void SaveScreens();
  52. extern void ReleaseActiveGrabs();
  53. extern void QueryFont();
  54. extern void NotImplemented();
  55. extern WindowPtr RealChildHead();
  56. extern Bool InitClientResources();
  57.  
  58. Selection *CurrentSelections;
  59. int NumCurrentSelections;
  60.  
  61. extern long ScreenSaverTime;
  62. extern long ScreenSaverInterval;
  63. extern int  ScreenSaverBlanking;
  64. extern int  ScreenSaverAllowExposures;
  65. extern long defaultScreenSaverTime;
  66. extern long defaultScreenSaverInterval;
  67. extern int  defaultScreenSaverBlanking;
  68. extern int  defaultScreenSaverAllowExposures;
  69. static ClientPtr onlyClient;
  70. static Bool grabbingClient = FALSE;
  71. static long *checkForInput[2];
  72. extern int connBlockScreenStart;
  73.  
  74. extern int (* InitialVector[3]) ();
  75. extern int (* ProcVector[256]) ();
  76. extern int (* SwappedProcVector[256]) ();
  77. extern void (* EventSwapVector[128]) ();
  78. extern void (* ReplySwapVector[256]) ();
  79. extern void Swap32Write(), SLHostsExtend(), SQColorsExtend(), WriteSConnectionInfo();
  80. extern void WriteSConnSetupPrefix();
  81. extern char *ClientAuthorized();
  82. extern Bool InsertFakeRequest();
  83. static void KillAllClients();
  84. static void DeleteClientFromAnySelections();
  85.  
  86. static int nextFreeClientID; /* always MIN free client ID */
  87.  
  88. static int    nClients;    /* number active clients */
  89.  
  90. char dispatchException = 0;
  91. char isItTimeToYield;
  92.  
  93. /* Various of the DIX function interfaces were not designed to allow
  94.  * the client->errorValue to be set on BadValue and other errors.
  95.  * Rather than changing interfaces and breaking untold code we introduce
  96.  * a new global that dispatch can use.
  97.  */
  98. XID clientErrorValue;   /* XXX this is a kludge */
  99.  
  100. #define SAME_SCREENS(a, b) (\
  101.     (a.pScreen == b.pScreen))
  102.  
  103. void
  104. SetInputCheck(c0, c1)
  105.     long *c0, *c1;
  106. {
  107.     checkForInput[0] = c0;
  108.     checkForInput[1] = c1;
  109. }
  110.  
  111. void
  112. UpdateCurrentTime()
  113. {
  114.     TimeStamp systime;
  115.  
  116.     /* To avoid time running backwards, we must call GetTimeInMillis before
  117.      * calling ProcessInputEvents.
  118.      */
  119.     systime.months = currentTime.months;
  120.     systime.milliseconds = GetTimeInMillis();
  121.     if (systime.milliseconds < currentTime.milliseconds)
  122.     systime.months++;
  123.     if (*checkForInput[0] != *checkForInput[1])
  124.     ProcessInputEvents();
  125.     if (CompareTimeStamps(systime, currentTime) == LATER)
  126.     currentTime = systime;
  127. }
  128.  
  129. /* Like UpdateCurrentTime, but can't call ProcessInputEvents */
  130. void
  131. UpdateCurrentTimeIf()
  132. {
  133.     TimeStamp systime;
  134.  
  135.     systime.months = currentTime.months;
  136.     systime.milliseconds = GetTimeInMillis();
  137.     if (systime.milliseconds < currentTime.milliseconds)
  138.     systime.months++;
  139.     if (*checkForInput[0] == *checkForInput[1])
  140.     currentTime = systime;
  141. }
  142.  
  143. void
  144. InitSelections()
  145. {
  146.     if (CurrentSelections)
  147.     xfree(CurrentSelections);
  148.     CurrentSelections = (Selection *)NULL;
  149.     NumCurrentSelections = 0;
  150. }
  151.  
  152. void 
  153. FlushClientCaches(id)
  154.     XID id;
  155. {
  156.     int i;
  157.     register ClientPtr client;
  158.  
  159.     client = clients[CLIENT_ID(id)];
  160.     if (client == NullClient)
  161.         return ;
  162.     for (i=0; i<currentMaxClients; i++)
  163.     {
  164.     client = clients[i];
  165.         if (client != NullClient)
  166.     {
  167.             if (client->lastDrawableID == id)
  168.         {
  169.                 client->lastDrawableID = INVALID;
  170.         client->lastDrawable = (DrawablePtr)NULL;
  171.         }
  172.             else if (client->lastGCID == id)
  173.         {
  174.                 client->lastGCID = INVALID;
  175.         client->lastGC = (GCPtr)NULL;
  176.         }
  177.     }
  178.     }
  179. }
  180.  
  181. #define MAJOROP ((xReq *)client->requestBuffer)->reqType
  182.  
  183. Dispatch()
  184. {
  185.     register int        *clientReady;     /* array of request ready clients */
  186.     register int    result;
  187.     register ClientPtr    client;
  188.     register int    nready;
  189.     register long    **icheck = checkForInput;
  190.  
  191.     nextFreeClientID = 1;
  192.     InitSelections();
  193.     nClients = 0;
  194.  
  195.     clientReady = (int *) ALLOCATE_LOCAL(sizeof(int) * MaxClients);
  196.     if (!clientReady)
  197.     return;
  198.  
  199.     while (!dispatchException)
  200.     {
  201.         if (*icheck[0] != *icheck[1])
  202.     {
  203.         ProcessInputEvents();
  204.         FlushIfCriticalOutputPending();
  205.     }
  206.  
  207.     nready = WaitForSomething(clientReady);
  208.  
  209.        /***************** 
  210.     *  Handle events in round robin fashion, doing input between 
  211.     *  each round 
  212.     *****************/
  213.  
  214.     while (!dispatchException && (--nready >= 0))
  215.     {
  216.         client = clients[clientReady[nready]];
  217.         if (! client)
  218.         {
  219.         /* KillClient can cause this to happen */
  220.         continue;
  221.         }
  222.         /* GrabServer activation can cause this to be true */
  223.         if (grabbingClient && (client != onlyClient))
  224.         break;
  225.         isItTimeToYield = FALSE;
  226.  
  227.             requestingClient = client;
  228.         while (!isItTimeToYield)
  229.         {
  230.             if (*icheck[0] != *icheck[1])
  231.         {
  232.             ProcessInputEvents();
  233.             FlushIfCriticalOutputPending();
  234.         }
  235.        
  236.         /* now, finally, deal with client requests */
  237.  
  238.             result = ReadRequestFromClient(client);
  239.             if (result <= 0) 
  240.             {
  241.             if (result < 0)
  242.             CloseDownClient(client);
  243.             break;
  244.             }
  245.  
  246.         client->sequence++;
  247. #ifdef DEBUG
  248.         if (client->requestLogIndex == MAX_REQUEST_LOG)
  249.             client->requestLogIndex = 0;
  250.         client->requestLog[client->requestLogIndex] = MAJOROP;
  251.         client->requestLogIndex++;
  252. #endif
  253.         if (result > (MAX_REQUEST_SIZE << 2))
  254.             result = BadLength;
  255.         else
  256.             result = (* client->requestVector[MAJOROP])(client);
  257.         
  258.         if (result != Success) 
  259.         {
  260.             if (client->noClientException != Success)
  261.                         CloseDownClient(client);
  262.                     else
  263.                 SendErrorToClient(client, MAJOROP,
  264.                       MinorOpcodeOfRequest(client),
  265.                       client->errorValue, result);
  266.             break;
  267.             }
  268.         }
  269.         FlushAllOutput();
  270.     }
  271.     }
  272.     KillAllClients();
  273.     DEALLOCATE_LOCAL(clientReady);
  274.     dispatchException &= ~DE_RESET;
  275. }
  276.  
  277. #undef MAJOROP
  278.  
  279. /*ARGSUSED*/
  280. int
  281. ProcBadRequest(client)
  282.     ClientPtr client;
  283. {
  284.     return (BadRequest);
  285. }
  286.  
  287. extern int Ones();
  288.  
  289. int
  290. ProcCreateWindow(client)
  291.     register ClientPtr client;
  292. {
  293.     register WindowPtr pParent, pWin;
  294.     REQUEST(xCreateWindowReq);
  295.     int result;
  296.     int len;
  297.  
  298.     REQUEST_AT_LEAST_SIZE(xCreateWindowReq);
  299.     
  300.     LEGAL_NEW_RESOURCE(stuff->wid, client);
  301.     if (!(pParent = (WindowPtr)LookupWindow(stuff->parent, client)))
  302.         return BadWindow;
  303.     len = stuff->length -  (sizeof(xCreateWindowReq) >> 2);
  304.     if (Ones(stuff->mask) != len)
  305.         return BadLength;
  306.     if (!stuff->width || !stuff->height)
  307.     {
  308.     client->errorValue = 0;
  309.         return BadValue;
  310.     }
  311.     pWin = CreateWindow(stuff->wid, pParent, stuff->x,
  312.                   stuff->y, stuff->width, stuff->height, 
  313.                   stuff->borderWidth, stuff->class,
  314.                   stuff->mask, (XID *) &stuff[1], 
  315.                   (int)stuff->depth, 
  316.                   client, stuff->visual, &result);
  317.     if (pWin)
  318.     {
  319.     Mask mask = pWin->eventMask;
  320.  
  321.     pWin->eventMask = 0; /* subterfuge in case AddResource fails */
  322.     if (!AddResource(stuff->wid, RT_WINDOW, (pointer)pWin))
  323.         return BadAlloc;
  324.     pWin->eventMask = mask;
  325.     }
  326.     if (client->noClientException != Success)
  327.         return(client->noClientException);
  328.     else
  329.         return(result);
  330. }
  331.  
  332. int
  333. ProcChangeWindowAttributes(client)
  334.     register ClientPtr client;
  335. {
  336.     register WindowPtr pWin;
  337.     REQUEST(xChangeWindowAttributesReq);
  338.     register int result;
  339.     int len;
  340.  
  341.     REQUEST_AT_LEAST_SIZE(xChangeWindowAttributesReq);
  342.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  343.     if (!pWin)
  344.         return(BadWindow);
  345.     len = stuff->length - (sizeof(xChangeWindowAttributesReq) >> 2);
  346.     if (len != Ones(stuff->valueMask))
  347.         return BadLength;
  348.     result =  ChangeWindowAttributes(pWin, 
  349.                   stuff->valueMask, 
  350.                   (XID *) &stuff[1], 
  351.                   client);
  352.     if (client->noClientException != Success)
  353.         return(client->noClientException);
  354.     else
  355.         return(result);
  356. }
  357.  
  358. int
  359. ProcGetWindowAttributes(client)
  360.     register ClientPtr client;
  361. {
  362.     register WindowPtr pWin;
  363.     REQUEST(xResourceReq);
  364.  
  365.     REQUEST_SIZE_MATCH(xResourceReq);
  366.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  367.     if (!pWin)
  368.         return(BadWindow);
  369.     GetWindowAttributes(pWin, client);
  370.     return(client->noClientException);
  371. }
  372.  
  373. int
  374. ProcDestroyWindow(client)
  375.     register ClientPtr client;
  376. {
  377.     register WindowPtr pWin;
  378.     REQUEST(xResourceReq);
  379.  
  380.     REQUEST_SIZE_MATCH(xResourceReq);
  381.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  382.     if (!pWin)
  383.         return(BadWindow);
  384.     if (pWin->parent)
  385.     FreeResource(stuff->id, RT_NONE);
  386.     return(client->noClientException);
  387. }
  388.  
  389. int
  390. ProcDestroySubwindows(client)
  391.     register ClientPtr client;
  392. {
  393.     register WindowPtr pWin;
  394.     REQUEST(xResourceReq);
  395.  
  396.     REQUEST_SIZE_MATCH(xResourceReq);
  397.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  398.     if (!pWin)
  399.         return(BadWindow);
  400.     DestroySubwindows(pWin, client);
  401.     return(client->noClientException);
  402. }
  403.  
  404. int
  405. ProcChangeSaveSet(client)
  406.     register ClientPtr client;
  407. {
  408.     register WindowPtr pWin;
  409.     REQUEST(xChangeSaveSetReq);
  410.     register result;
  411.           
  412.     REQUEST_SIZE_MATCH(xChangeSaveSetReq);
  413.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  414.     if (!pWin)
  415.         return(BadWindow);
  416.     if (client->clientAsMask == (CLIENT_BITS(pWin->drawable.id)))
  417.         return BadMatch;
  418.     if ((stuff->mode == SetModeInsert) || (stuff->mode == SetModeDelete))
  419.     {
  420.         result = AlterSaveSetForClient(client, pWin, stuff->mode);
  421.     if (client->noClientException != Success)
  422.         return(client->noClientException);
  423.     else
  424.             return(result);
  425.     }
  426.     else
  427.     {
  428.     client->errorValue = stuff->mode;
  429.     return( BadValue );
  430.     }
  431. }
  432.  
  433. int
  434. ProcReparentWindow(client)
  435.     register ClientPtr client;
  436. {
  437.     register WindowPtr pWin, pParent;
  438.     REQUEST(xReparentWindowReq);
  439.     register int result;
  440.  
  441.     REQUEST_SIZE_MATCH(xReparentWindowReq);
  442.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  443.     if (!pWin)
  444.         return(BadWindow);
  445.     pParent = (WindowPtr)LookupWindow(stuff->parent, client);
  446.     if (!pParent)
  447.         return(BadWindow);
  448.     if (SAME_SCREENS(pWin->drawable, pParent->drawable))
  449.     {
  450.         if ((pWin->backgroundState == ParentRelative) &&
  451.             (pParent->drawable.depth != pWin->drawable.depth))
  452.             return BadMatch;
  453.         result =  ReparentWindow(pWin, pParent, 
  454.              (short)stuff->x, (short)stuff->y, client);
  455.     if (client->noClientException != Success)
  456.             return(client->noClientException);
  457.     else
  458.             return(result);
  459.     }
  460.     else 
  461.         return (BadMatch);
  462. }
  463.  
  464. int
  465. ProcMapWindow(client)
  466.     register ClientPtr client;
  467. {
  468.     register WindowPtr pWin;
  469.     REQUEST(xResourceReq);
  470.  
  471.     REQUEST_SIZE_MATCH(xResourceReq);
  472.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  473.     if (!pWin)
  474.         return(BadWindow);
  475.     MapWindow(pWin, client);
  476.            /* update cache to say it is mapped */
  477.     return(client->noClientException);
  478. }
  479.  
  480. int
  481. ProcMapSubwindows(client)
  482.     register ClientPtr client;
  483. {
  484.     register WindowPtr pWin;
  485.     REQUEST(xResourceReq);
  486.  
  487.     REQUEST_SIZE_MATCH(xResourceReq);
  488.     pWin = (WindowPtr)LookupWindow( stuff->id, client);
  489.     if (!pWin)
  490.         return(BadWindow);
  491.     MapSubwindows(pWin, client);
  492.            /* update cache to say it is mapped */
  493.     return(client->noClientException);
  494. }
  495.  
  496. int
  497. ProcUnmapWindow(client)
  498.     register ClientPtr client;
  499. {
  500.     register WindowPtr pWin;
  501.     REQUEST(xResourceReq);
  502.  
  503.     REQUEST_SIZE_MATCH(xResourceReq);
  504.     pWin = (WindowPtr)LookupWindow( stuff->id, client);
  505.     if (!pWin)
  506.         return(BadWindow);
  507.     UnmapWindow(pWin, FALSE);
  508.            /* update cache to say it is mapped */
  509.     return(client->noClientException);
  510. }
  511.  
  512. int
  513. ProcUnmapSubwindows(client)
  514.     register ClientPtr client;
  515. {
  516.     register WindowPtr pWin;
  517.     REQUEST(xResourceReq);
  518.  
  519.     REQUEST_SIZE_MATCH(xResourceReq);
  520.     pWin = (WindowPtr)LookupWindow( stuff->id, client);
  521.     if (!pWin)
  522.         return(BadWindow);
  523.     UnmapSubwindows(pWin);
  524.     return(client->noClientException);
  525. }
  526.  
  527. int
  528. ProcConfigureWindow(client)
  529.     register ClientPtr client;
  530. {
  531.     register WindowPtr pWin;
  532.     REQUEST(xConfigureWindowReq);
  533.     register int result;
  534.     int len;
  535.  
  536.     REQUEST_AT_LEAST_SIZE(xConfigureWindowReq);
  537.     pWin = (WindowPtr)LookupWindow( stuff->window, client);
  538.     if (!pWin)
  539.         return(BadWindow);
  540.     len = stuff->length - (sizeof(xConfigureWindowReq) >> 2);
  541.     if (Ones((Mask)stuff->mask) != len)
  542.         return BadLength;
  543.     result =  ConfigureWindow(pWin, (Mask)stuff->mask, (XID *) &stuff[1], 
  544.                   client);
  545.     if (client->noClientException != Success)
  546.         return(client->noClientException);
  547.     else
  548.         return(result);
  549. }
  550.  
  551. int
  552. ProcCirculateWindow(client)
  553.     register ClientPtr client;
  554. {
  555.     register WindowPtr pWin;
  556.     REQUEST(xCirculateWindowReq);
  557.  
  558.     REQUEST_SIZE_MATCH(xCirculateWindowReq);
  559.     if ((stuff->direction != RaiseLowest) &&
  560.     (stuff->direction != LowerHighest))
  561.     {
  562.     client->errorValue = stuff->direction;
  563.         return BadValue;
  564.     }
  565.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  566.     if (!pWin)
  567.         return(BadWindow);
  568.     CirculateWindow(pWin, (int)stuff->direction, client);
  569.     return(client->noClientException);
  570. }
  571.  
  572. int
  573. ProcGetGeometry(client)
  574.     register ClientPtr client;
  575. {
  576.     xGetGeometryReply rep;
  577.     register DrawablePtr pDraw;
  578.     REQUEST(xResourceReq);
  579.  
  580.     REQUEST_SIZE_MATCH(xResourceReq);
  581.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->id, client)))
  582.     {                /* can be inputonly */
  583.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->id, client))) 
  584.             return (BadDrawable);
  585.     }
  586.     rep.type = X_Reply;
  587.     rep.length = 0;
  588.     rep.sequenceNumber = client->sequence;
  589.     rep.root = WindowTable[pDraw->pScreen->myNum]->drawable.id;
  590.     rep.depth = pDraw->depth;
  591.  
  592.     rep.width = pDraw->width;
  593.     rep.height = pDraw->height;
  594.     if (pDraw->type == DRAWABLE_PIXMAP)
  595.     {
  596.     rep.x = rep.y = rep.borderWidth = 0;
  597.     }
  598.     else
  599.     {
  600.         register WindowPtr pWin = (WindowPtr)pDraw;
  601.     rep.x = pWin->origin.x - wBorderWidth (pWin);
  602.     rep.y = pWin->origin.y - wBorderWidth (pWin);
  603.     rep.borderWidth = pWin->borderWidth;
  604.     }
  605.     WriteReplyToClient(client, sizeof(xGetGeometryReply), &rep);
  606.     return(client->noClientException);
  607. }
  608.  
  609. int
  610. ProcQueryTree(client)
  611.     register ClientPtr client;
  612. {
  613.  
  614.     xQueryTreeReply reply;
  615.     int numChildren = 0;
  616.     register WindowPtr pChild, pWin, pHead;
  617.     Window  *childIDs = (Window *)NULL;
  618.     REQUEST(xResourceReq);
  619.  
  620.     REQUEST_SIZE_MATCH(xResourceReq);
  621.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  622.     if (!pWin)
  623.         return(BadWindow);
  624.     reply.type = X_Reply;
  625.     reply.root = WindowTable[pWin->drawable.pScreen->myNum]->drawable.id;
  626.     reply.sequenceNumber = client->sequence;
  627.     if (pWin->parent)
  628.     reply.parent = pWin->parent->drawable.id;
  629.     else
  630.         reply.parent = (Window)None;
  631.  
  632.     pHead = RealChildHead(pWin);
  633.     for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  634.     numChildren++;
  635.     if (numChildren)
  636.     {
  637.     int curChild = 0;
  638.  
  639.     childIDs = (Window *) ALLOCATE_LOCAL(numChildren * sizeof(Window));
  640.     if (!childIDs)
  641.         return BadAlloc;
  642.     for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  643.         childIDs[curChild++] = pChild->drawable.id;
  644.     }
  645.     
  646.     reply.nChildren = numChildren;
  647.     reply.length = (numChildren * sizeof(Window)) >> 2;
  648.     
  649.     WriteReplyToClient(client, sizeof(xQueryTreeReply), &reply);
  650.     if (numChildren)
  651.     {
  652.         client->pSwapReplyFunc = Swap32Write;
  653.     WriteSwappedDataToClient(client, numChildren * sizeof(Window), childIDs);
  654.     DEALLOCATE_LOCAL(childIDs);
  655.     }
  656.  
  657.     return(client->noClientException);
  658. }
  659.  
  660. int
  661. ProcInternAtom(client)
  662.     register ClientPtr client;
  663. {
  664.     Atom atom;
  665.     char *tchar;
  666.     REQUEST(xInternAtomReq);
  667.  
  668.     REQUEST_FIXED_SIZE(xInternAtomReq, stuff->nbytes);
  669.     if ((stuff->onlyIfExists != xTrue) && (stuff->onlyIfExists != xFalse))
  670.     {
  671.     client->errorValue = stuff->onlyIfExists;
  672.         return(BadValue);
  673.     }
  674.     tchar = (char *) &stuff[1];
  675.     atom = MakeAtom(tchar, stuff->nbytes, !stuff->onlyIfExists);
  676.     if (atom != BAD_RESOURCE)
  677.     {
  678.     xInternAtomReply reply;
  679.     reply.type = X_Reply;
  680.     reply.length = 0;
  681.     reply.sequenceNumber = client->sequence;
  682.     reply.atom = atom;
  683.     WriteReplyToClient(client, sizeof(xInternAtomReply), &reply);
  684.     return(client->noClientException);
  685.     }
  686.     else
  687.     return (BadAlloc);
  688. }
  689.  
  690. int
  691. ProcGetAtomName(client)
  692.     register ClientPtr client;
  693. {
  694.     char *str;
  695.     xGetAtomNameReply reply;
  696.     int len;
  697.     REQUEST(xResourceReq);
  698.  
  699.     REQUEST_SIZE_MATCH(xResourceReq);
  700.     if (str = NameForAtom(stuff->id)) 
  701.     {
  702.     len = strlen(str);
  703.     reply.type = X_Reply;
  704.     reply.length = (len + 3) >> 2;
  705.     reply.sequenceNumber = client->sequence;
  706.     reply.nameLength = len;
  707.     WriteReplyToClient(client, sizeof(xGetAtomNameReply), &reply);
  708.     (void)WriteToClient(client, len, str);
  709.     return(client->noClientException);
  710.     }
  711.     else 
  712.     { 
  713.     client->errorValue = stuff->id;
  714.     return (BadAtom);
  715.     }
  716. }
  717.  
  718. int 
  719. ProcDeleteProperty(client)
  720.     register ClientPtr client;
  721. {
  722.     WindowPtr pWin;
  723.     REQUEST(xDeletePropertyReq);
  724.     int result;
  725.               
  726.     REQUEST_SIZE_MATCH(xDeletePropertyReq);
  727.     UpdateCurrentTime();
  728.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  729.     if (!pWin)
  730.         return(BadWindow);
  731.     if (ValidAtom(stuff->property))
  732.     {
  733.     result = DeleteProperty(pWin, stuff->property);
  734.         if (client->noClientException != Success)
  735.             return(client->noClientException);
  736.     else
  737.         return(result);
  738.     }
  739.     else 
  740.     {
  741.     client->errorValue = stuff->property;
  742.     return (BadAtom);
  743.     }
  744. }
  745.  
  746.  
  747. int
  748. ProcSetSelectionOwner(client)
  749.     register ClientPtr client;
  750. {
  751.     WindowPtr pWin;
  752.     TimeStamp time;
  753.     REQUEST(xSetSelectionOwnerReq);
  754.  
  755.     REQUEST_SIZE_MATCH(xSetSelectionOwnerReq);
  756.     UpdateCurrentTime();
  757.     time = ClientTimeToServerTime(stuff->time);
  758.  
  759.     /* If the client's time stamp is in the future relative to the server's
  760.     time stamp, do not set the selection, just return success. */
  761.     if (CompareTimeStamps(time, currentTime) == LATER)
  762.         return Success;
  763.     if (stuff->window != None)
  764.     {
  765.         pWin = (WindowPtr)LookupWindow(stuff->window, client);
  766.         if (!pWin)
  767.             return(BadWindow);
  768.     }
  769.     else
  770.         pWin = (WindowPtr)None;
  771.     if (ValidAtom(stuff->selection))
  772.     {
  773.     int i = 0;
  774.  
  775.     /*
  776.      * First, see if the selection is already set... 
  777.      */
  778.     while ((i < NumCurrentSelections) && 
  779.            CurrentSelections[i].selection != stuff->selection) 
  780.             i++;
  781.         if (i < NumCurrentSelections)
  782.         {        
  783.         xEvent event;
  784.  
  785.         /* If the timestamp in client's request is in the past relative
  786.         to the time stamp indicating the last time the owner of the
  787.         selection was set, do not set the selection, just return 
  788.         success. */
  789.             if (CompareTimeStamps(time, CurrentSelections[i].lastTimeChanged)
  790.         == EARLIER)
  791.         return Success;
  792.         if ((CurrentSelections[i].window != None) &&
  793.         (CurrentSelections[i].client != client))
  794.         {
  795.         event.u.u.type = SelectionClear;
  796.         event.u.selectionClear.time = time.milliseconds;
  797.         event.u.selectionClear.window = CurrentSelections[i].window;
  798.         event.u.selectionClear.atom = CurrentSelections[i].selection;
  799.         (void) TryClientEvents (CurrentSelections[i].client, &event, 1,
  800.                 NoEventMask, NoEventMask /* CantBeFiltered */,
  801.                 NullGrab);
  802.         }
  803.     }
  804.     else
  805.     {
  806.         /*
  807.          * It doesn't exist, so add it...
  808.          */
  809.         Selection *newsels;
  810.  
  811.         if (i == 0)
  812.         newsels = (Selection *)xalloc(sizeof(Selection));
  813.         else
  814.         newsels = (Selection *)xrealloc(CurrentSelections,
  815.                 (NumCurrentSelections + 1) * sizeof(Selection));
  816.         if (!newsels)
  817.         return BadAlloc;
  818.         NumCurrentSelections++;
  819.         CurrentSelections = newsels;
  820.         CurrentSelections[i].selection = stuff->selection;
  821.     }
  822.         CurrentSelections[i].lastTimeChanged = time;
  823.     CurrentSelections[i].window = stuff->window;
  824.     CurrentSelections[i].pWin = pWin;
  825.     CurrentSelections[i].client = client;
  826.     return (client->noClientException);
  827.     }
  828.     else 
  829.     {
  830.     client->errorValue = stuff->selection;
  831.         return (BadAtom);
  832.     }
  833. }
  834.  
  835. int
  836. ProcGetSelectionOwner(client)
  837.     register ClientPtr client;
  838. {
  839.     REQUEST(xResourceReq);
  840.  
  841.     REQUEST_SIZE_MATCH(xResourceReq);
  842.     if (ValidAtom(stuff->id))
  843.     {
  844.     int i;
  845.         xGetSelectionOwnerReply reply;
  846.  
  847.     i = 0;
  848.         while ((i < NumCurrentSelections) && 
  849.            CurrentSelections[i].selection != stuff->id) i++;
  850.         reply.type = X_Reply;
  851.     reply.length = 0;
  852.     reply.sequenceNumber = client->sequence;
  853.         if (i < NumCurrentSelections)
  854.             reply.owner = CurrentSelections[i].window;
  855.         else
  856.             reply.owner = None;
  857.         WriteReplyToClient(client, sizeof(xGetSelectionOwnerReply), &reply);
  858.         return(client->noClientException);
  859.     }
  860.     else            
  861.     {
  862.     client->errorValue = stuff->id;
  863.         return (BadAtom); 
  864.     }
  865. }
  866.  
  867. int
  868. ProcConvertSelection(client)
  869.     register ClientPtr client;
  870. {
  871.     Bool paramsOkay;
  872.     xEvent event;
  873.     WindowPtr pWin;
  874.     REQUEST(xConvertSelectionReq);
  875.  
  876.     REQUEST_SIZE_MATCH(xConvertSelectionReq);
  877.     pWin = (WindowPtr)LookupWindow(stuff->requestor, client);
  878.     if (!pWin)
  879.         return(BadWindow);
  880.  
  881.     paramsOkay = (ValidAtom(stuff->selection) && ValidAtom(stuff->target));
  882.     if (stuff->property != None)
  883.     paramsOkay &= ValidAtom(stuff->property);
  884.     if (paramsOkay)
  885.     {
  886.     int i;
  887.  
  888.     i = 0;
  889.     while ((i < NumCurrentSelections) && 
  890.            CurrentSelections[i].selection != stuff->selection) i++;
  891.     if ((i < NumCurrentSelections) && 
  892.         (CurrentSelections[i].window != None))
  893.     {        
  894.         event.u.u.type = SelectionRequest;
  895.         event.u.selectionRequest.time = stuff->time;
  896.         event.u.selectionRequest.owner = 
  897.             CurrentSelections[i].window;
  898.         event.u.selectionRequest.requestor = stuff->requestor;
  899.         event.u.selectionRequest.selection = stuff->selection;
  900.         event.u.selectionRequest.target = stuff->target;
  901.         event.u.selectionRequest.property = stuff->property;
  902.         if (TryClientEvents(
  903.         CurrentSelections[i].client, &event, 1, NoEventMask,
  904.         NoEventMask /* CantBeFiltered */, NullGrab))
  905.         return (client->noClientException);
  906.     }
  907.     event.u.u.type = SelectionNotify;
  908.     event.u.selectionNotify.time = stuff->time;
  909.     event.u.selectionNotify.requestor = stuff->requestor;
  910.     event.u.selectionNotify.selection = stuff->selection;
  911.     event.u.selectionNotify.target = stuff->target;
  912.     event.u.selectionNotify.property = None;
  913.     (void) TryClientEvents(client, &event, 1, NoEventMask,
  914.                    NoEventMask /* CantBeFiltered */, NullGrab);
  915.     return (client->noClientException);
  916.     }
  917.     else 
  918.     {
  919.     client->errorValue = stuff->property;
  920.         return (BadAtom);
  921.     }
  922. }
  923.  
  924. int
  925. ProcGrabServer(client)
  926.     register ClientPtr client;
  927. {
  928.     OnlyListenToOneClient(client);
  929.     grabbingClient = TRUE;
  930.     onlyClient = client;
  931.     return(client->noClientException);
  932. }
  933.  
  934. int
  935. ProcUngrabServer(client)
  936.     register ClientPtr client;
  937. {
  938.     REQUEST(xReq);
  939.     REQUEST_SIZE_MATCH(xReq);
  940.     grabbingClient = FALSE;
  941.     ListenToAllClients();
  942.     return(client->noClientException);
  943. }
  944.  
  945. int
  946. ProcTranslateCoords(client)
  947.     register ClientPtr client;
  948. {
  949.     REQUEST(xTranslateCoordsReq);
  950.  
  951.     register WindowPtr pWin, pDst;
  952.     xTranslateCoordsReply rep;
  953.  
  954.     REQUEST_SIZE_MATCH(xTranslateCoordsReq);
  955.     pWin = (WindowPtr)LookupWindow(stuff->srcWid, client);
  956.     if (!pWin)
  957.         return(BadWindow);
  958.     pDst = (WindowPtr)LookupWindow(stuff->dstWid, client);
  959.     if (!pDst)
  960.         return(BadWindow);
  961.     rep.type = X_Reply;
  962.     rep.length = 0;
  963.     rep.sequenceNumber = client->sequence;
  964.     if (!SAME_SCREENS(pWin->drawable, pDst->drawable))
  965.     {
  966.     rep.sameScreen = xFalse;
  967.         rep.child = None;
  968.     rep.dstX = rep.dstY = 0;
  969.     }
  970.     else
  971.     {
  972.     INT16 x, y;
  973.     rep.sameScreen = xTrue;
  974.     rep.child = None;
  975.     /* computing absolute coordinates -- adjust to destination later */
  976.     x = pWin->drawable.x + stuff->srcX;
  977.     y = pWin->drawable.y + stuff->srcY;
  978.     pWin = pDst->firstChild;
  979.     while (pWin)
  980.     {
  981. #ifdef SHAPE
  982.         BoxRec  box;
  983. #endif
  984.         if ((pWin->mapped) &&
  985.         (x >= pWin->drawable.x - wBorderWidth (pWin)) &&
  986.         (x < pWin->drawable.x + (int)pWin->drawable.width +
  987.          wBorderWidth (pWin)) &&
  988.         (y >= pWin->drawable.y - wBorderWidth (pWin)) &&
  989.         (y < pWin->drawable.y + (int)pWin->drawable.height +
  990.          wBorderWidth (pWin))
  991. #ifdef SHAPE
  992.         /* When a window is shaped, a further check
  993.          * is made to see if the point is inside
  994.          * borderSize
  995.          */
  996.         && (!wBoundingShape(pWin) ||
  997.             (*pWin->drawable.pScreen->PointInRegion)
  998.                 (&pWin->borderSize, x, y, &box))
  999. #endif
  1000.         )
  1001.             {
  1002.         rep.child = pWin->drawable.id;
  1003.         pWin = (WindowPtr) NULL;
  1004.         }
  1005.         else
  1006.         pWin = pWin->nextSib;
  1007.     }
  1008.     /* adjust to destination coordinates */
  1009.     rep.dstX = x - pDst->drawable.x;
  1010.     rep.dstY = y - pDst->drawable.y;
  1011.     }
  1012.     WriteReplyToClient(client, sizeof(xTranslateCoordsReply), &rep);
  1013.     return(client->noClientException);
  1014. }
  1015.  
  1016. int
  1017. ProcOpenFont(client)
  1018.     register ClientPtr client;
  1019. {
  1020.     FontPtr pFont;
  1021.     REQUEST(xOpenFontReq);
  1022.  
  1023.     REQUEST_FIXED_SIZE(xOpenFontReq, stuff->nbytes);
  1024.     client->errorValue = stuff->fid;
  1025.     LEGAL_NEW_RESOURCE(stuff->fid, client);
  1026.     if ( pFont = OpenFont( stuff->nbytes, (char *)&stuff[1]))
  1027.     {
  1028.     if (!AddResource(stuff->fid, RT_FONT, (pointer)pFont))
  1029.         return BadAlloc;
  1030.     return(client->noClientException);
  1031.     }
  1032.     else
  1033.     return (BadName);
  1034. }
  1035.  
  1036. int
  1037. ProcCloseFont(client)
  1038.     register ClientPtr client;
  1039. {
  1040.     FontPtr pFont;
  1041.     REQUEST(xResourceReq);
  1042.  
  1043.     REQUEST_SIZE_MATCH(xResourceReq);
  1044.     pFont = (FontPtr)LookupIDByType(stuff->id, RT_FONT);
  1045.     if ( pFont != (FontPtr)NULL)    /* id was valid */
  1046.     {
  1047.         FreeResource(stuff->id, RT_NONE);
  1048.     return(client->noClientException);
  1049.     }
  1050.     else
  1051.     {
  1052.     client->errorValue = stuff->id;
  1053.         return (BadFont);
  1054.     }
  1055. }
  1056.  
  1057. int
  1058. ProcQueryFont(client)
  1059.     register ClientPtr client;
  1060. {
  1061.     xQueryFontReply    *reply;
  1062.     FontPtr pFont;
  1063.     register GC *pGC;
  1064.     REQUEST(xResourceReq);
  1065.  
  1066.     REQUEST_SIZE_MATCH(xResourceReq);
  1067.     client->errorValue = stuff->id;        /* EITHER font or gc */
  1068.     pFont = (FontPtr)LookupIDByType(stuff->id, RT_FONT);
  1069.     if (!pFont)
  1070.     {
  1071.       /* can't use VERIFY_GC because it might return BadGC */
  1072.     pGC = (GC *) LookupIDByType(stuff->id, RT_GC);
  1073.         if (!pGC)
  1074.     {
  1075.         client->errorValue = stuff->id;
  1076.             return(BadFont);     /* procotol spec says only error is BadFont */
  1077.     }
  1078.     pFont = pGC->font;
  1079.     }
  1080.  
  1081.     {
  1082.     CharInfoPtr    pmax = pFont->pInkMax;
  1083.     CharInfoPtr    pmin = pFont->pInkMin;
  1084.     int        nprotoxcistructs;
  1085.     int        rlength;
  1086.  
  1087.     nprotoxcistructs = (
  1088.        pmax->metrics.rightSideBearing == pmin->metrics.rightSideBearing &&
  1089.        pmax->metrics.leftSideBearing == pmin->metrics.leftSideBearing &&
  1090.        pmax->metrics.descent == pmin->metrics.descent &&
  1091.        pmax->metrics.ascent == pmin->metrics.ascent &&
  1092.        pmax->metrics.characterWidth == pmin->metrics.characterWidth) ?
  1093.         0 : n2dChars(pFont->pFI);
  1094.  
  1095.     rlength = sizeof(xQueryFontReply) +
  1096.                  pFont->pFI->nProps * sizeof(xFontProp)  +
  1097.              nprotoxcistructs * sizeof(xCharInfo);
  1098.     reply = (xQueryFontReply *)ALLOCATE_LOCAL(rlength);
  1099.     if(!reply)
  1100.     {
  1101.         return(BadAlloc);
  1102.     }
  1103.  
  1104.     reply->type = X_Reply;
  1105.     reply->length = (rlength - sizeof(xGenericReply)) >> 2;
  1106.     reply->sequenceNumber = client->sequence;
  1107.     QueryFont( pFont, reply, nprotoxcistructs);
  1108.  
  1109.         WriteReplyToClient(client, rlength, reply);
  1110.     DEALLOCATE_LOCAL(reply);
  1111.     return(client->noClientException);
  1112.     }
  1113. }
  1114.  
  1115. int
  1116. ProcQueryTextExtents(client)
  1117.     register ClientPtr client;
  1118. {
  1119.     REQUEST(xQueryTextExtentsReq);
  1120.     xQueryTextExtentsReply reply;
  1121.     FontPtr pFont;
  1122.     GC *pGC;
  1123.     ExtentInfoRec info;
  1124.     unsigned long length;
  1125.  
  1126.     REQUEST_AT_LEAST_SIZE(xQueryTextExtentsReq);
  1127.         
  1128.     pFont = (FontPtr)LookupIDByType(stuff->fid, RT_FONT);
  1129.     if (!pFont)
  1130.     {
  1131.         pGC = (GC *)LookupIDByType(stuff->fid, RT_GC);
  1132.         if (!pGC)
  1133.     {
  1134.         client->errorValue = stuff->fid;
  1135.             return(BadFont);
  1136.     }
  1137.     pFont = pGC->font;
  1138.     }
  1139.     length = stuff->length - (sizeof(xQueryTextExtentsReq) >> 2);
  1140.     length = length << 1;
  1141.     if (stuff->oddLength)
  1142.     {
  1143.     if (length == 0)
  1144.         return(BadLength);
  1145.         length--;
  1146.     }
  1147.     if (!QueryTextExtents(pFont, length, (unsigned char *)&stuff[1], &info))
  1148.     return(BadAlloc);
  1149.     reply.type = X_Reply;
  1150.     reply.length = 0;
  1151.     reply.sequenceNumber = client->sequence;
  1152.     reply.drawDirection = info.drawDirection;
  1153.     reply.fontAscent = info.fontAscent;
  1154.     reply.fontDescent = info.fontDescent;
  1155.     reply.overallAscent = info.overallAscent;
  1156.     reply.overallDescent = info.overallDescent;
  1157.     reply.overallWidth = info.overallWidth;
  1158.     reply.overallLeft = info.overallLeft;
  1159.     reply.overallRight = info.overallRight;
  1160.     WriteReplyToClient(client, sizeof(xQueryTextExtentsReply), &reply);
  1161.     return(client->noClientException);
  1162. }
  1163.  
  1164. int
  1165. ProcListFonts(client)
  1166.     register ClientPtr client;
  1167. {
  1168.     xListFontsReply reply; 
  1169.     FontPathPtr fpr;
  1170.     int stringLens, i;
  1171.     char *bufptr, *bufferStart;
  1172.     REQUEST(xListFontsReq);
  1173.  
  1174.     REQUEST_FIXED_SIZE(xListFontsReq, stuff->nbytes);
  1175.  
  1176.     fpr = ExpandFontNamePattern( stuff->nbytes, 
  1177.                  (char *) &stuff[1], stuff->maxNames);
  1178.     if (!fpr)
  1179.     return(BadAlloc);
  1180.     stringLens = 0;
  1181.     for (i=0; i<fpr->npaths; i++)
  1182.         stringLens += fpr->length[i];
  1183.  
  1184.     reply.type = X_Reply;
  1185.     reply.length = (stringLens + fpr->npaths + 3) >> 2;
  1186.     reply.nFonts = fpr->npaths;
  1187.     reply.sequenceNumber = client->sequence;
  1188.  
  1189.     bufptr = bufferStart = (char *)ALLOCATE_LOCAL(reply.length << 2);
  1190.     if(!bufptr && reply.length)
  1191.     {
  1192.     FreeFontRecord(fpr);
  1193.         return(BadAlloc);
  1194.     }
  1195.  
  1196.             /* since WriteToClient long word aligns things, 
  1197.            copy to temp buffer and write all at once */
  1198.     for (i=0; i<fpr->npaths; i++)
  1199.     {
  1200.         *bufptr++ = fpr->length[i];
  1201.         bcopy(fpr->paths[i], bufptr,  fpr->length[i]);
  1202.         bufptr += fpr->length[i];
  1203.     }
  1204.     WriteReplyToClient(client, sizeof(xListFontsReply), &reply);
  1205.     (void)WriteToClient(client, stringLens + fpr->npaths, bufferStart);
  1206.     FreeFontRecord(fpr);
  1207.     DEALLOCATE_LOCAL(bufferStart);
  1208.     
  1209.     return(client->noClientException);
  1210. }
  1211.  
  1212. int
  1213. ProcListFontsWithInfo(client)
  1214.     register ClientPtr client;
  1215. {
  1216.     register xListFontsWithInfoReply *reply, *nreply;
  1217.     xListFontsWithInfoReply last_reply;
  1218.     FontRec font;
  1219.     FontInfoRec finfo;
  1220.     register FontPathPtr fpaths;
  1221.     register char **path;
  1222.     register int n, *length;
  1223.     int curlength, rlength;
  1224.     REQUEST(xListFontsWithInfoReq);
  1225.  
  1226.     REQUEST_FIXED_SIZE(xListFontsWithInfoReq, stuff->nbytes);
  1227.  
  1228.     fpaths = ExpandFontNamePattern( stuff->nbytes,
  1229.                     (char *) &stuff[1], stuff->maxNames);
  1230.     if (!fpaths)
  1231.     return(BadAlloc);
  1232.     font.pFI = &finfo;
  1233.     font.pInkMin = &finfo.minbounds;
  1234.     font.pInkMax = &finfo.maxbounds;
  1235.     reply = (xListFontsWithInfoReply *)NULL;
  1236.     curlength = 0;
  1237.     for (n = fpaths->npaths, path = fpaths->paths, length = fpaths->length;
  1238.      --n >= 0;
  1239.      path++, length++)
  1240.     {
  1241.     if (!(DescribeFont(*path, *length, &finfo, &font.pFP)))
  1242.        continue;
  1243.     rlength = sizeof(xListFontsWithInfoReply)
  1244.             + finfo.nProps * sizeof(xFontProp);
  1245.     if (rlength > curlength)
  1246.     {
  1247.         nreply = (xListFontsWithInfoReply *) xalloc(rlength);
  1248.         if (nreply)
  1249.         {
  1250.         xfree (reply);
  1251.         reply = nreply;
  1252.         curlength = rlength;
  1253.         }
  1254.     }
  1255.     if (rlength <= curlength)
  1256.     {
  1257.         reply->type = X_Reply;
  1258.         reply->sequenceNumber = client->sequence;
  1259.         reply->length = (rlength - sizeof(xGenericReply)
  1260.                  + *length + 3) >> 2;
  1261.         QueryFont(&font, (xQueryFontReply *) reply, 0);
  1262.         reply->nameLength = *length;
  1263.         reply->nReplies = n;
  1264.         WriteReplyToClient(client, rlength, reply);
  1265.         (void)WriteToClient(client, *length, *path);
  1266.     }
  1267.     xfree(font.pFP);
  1268.     }
  1269.     xfree(reply);
  1270.     FreeFontRecord(fpaths);
  1271.     bzero((char *)&last_reply, sizeof(xListFontsWithInfoReply));
  1272.     last_reply.type = X_Reply;
  1273.     last_reply.sequenceNumber = client->sequence;
  1274.     last_reply.length = (sizeof(xListFontsWithInfoReply)
  1275.               - sizeof(xGenericReply)) >> 2;
  1276.     WriteReplyToClient(client, sizeof(xListFontsWithInfoReply), &last_reply);
  1277.     return(client->noClientException);
  1278. }
  1279.  
  1280. /*ARGSUSED*/
  1281. dixDestroyPixmap(pPixmap, pid)
  1282.     PixmapPtr pPixmap;
  1283.     Pixmap pid;
  1284. {
  1285.     (*pPixmap->drawable.pScreen->DestroyPixmap)(pPixmap);
  1286. }
  1287.  
  1288. int
  1289. ProcCreatePixmap(client)
  1290.     register ClientPtr client;
  1291. {
  1292.     PixmapPtr pMap;
  1293.     register DrawablePtr pDraw;
  1294.     REQUEST(xCreatePixmapReq);
  1295.     DepthPtr pDepth;
  1296.     register int i;
  1297.  
  1298.     REQUEST_SIZE_MATCH(xCreatePixmapReq);
  1299.     client->errorValue = stuff->pid;
  1300.     LEGAL_NEW_RESOURCE(stuff->pid, client);
  1301.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client)))
  1302.     {        /* can be inputonly */
  1303.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->drawable, client))) 
  1304.             return (BadDrawable);
  1305.     }
  1306.  
  1307.     if (!stuff->width || !stuff->height)
  1308.     {
  1309.     client->errorValue = 0;
  1310.         return BadValue;
  1311.     }
  1312.     if (stuff->depth != 1)
  1313.     {
  1314.         pDepth = pDraw->pScreen->allowedDepths;
  1315.         for (i=0; i<pDraw->pScreen->numDepths; i++, pDepth++)
  1316.        if (pDepth->depth == stuff->depth)
  1317.                goto CreatePmap;
  1318.     client->errorValue = stuff->depth;
  1319.         return BadValue;
  1320.     }
  1321. CreatePmap:
  1322.     pMap = (PixmapPtr)(*pDraw->pScreen->CreatePixmap)
  1323.         (pDraw->pScreen, stuff->width,
  1324.          stuff->height, stuff->depth);
  1325.     if (pMap)
  1326.     {
  1327.     pMap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  1328.     pMap->drawable.id = stuff->pid;
  1329.     if (AddResource(stuff->pid, RT_PIXMAP, (pointer)pMap))
  1330.         return(client->noClientException);
  1331.     }
  1332.     return (BadAlloc);
  1333. }
  1334.  
  1335. int
  1336. ProcFreePixmap(client)
  1337.     register ClientPtr client;
  1338. {
  1339.     PixmapPtr pMap;
  1340.  
  1341.     REQUEST(xResourceReq);
  1342.  
  1343.     REQUEST_SIZE_MATCH(xResourceReq);
  1344.     pMap = (PixmapPtr)LookupIDByType(stuff->id, RT_PIXMAP);
  1345.     if (pMap) 
  1346.     {
  1347.     FreeResource(stuff->id, RT_NONE);
  1348.     return(client->noClientException);
  1349.     }
  1350.     else 
  1351.     {
  1352.     client->errorValue = stuff->id;
  1353.     return (BadPixmap);
  1354.     }
  1355. }
  1356.  
  1357. int
  1358. ProcCreateGC(client)
  1359.     register ClientPtr client;
  1360. {
  1361.     int error;
  1362.     GC *pGC;
  1363.     register DrawablePtr pDraw;
  1364.     unsigned len;
  1365.     REQUEST(xCreateGCReq);
  1366.  
  1367.     REQUEST_AT_LEAST_SIZE(xCreateGCReq);
  1368.     client->errorValue = stuff->gc;
  1369.     LEGAL_NEW_RESOURCE(stuff->gc, client);
  1370.     if (!(pDraw = LOOKUP_DRAWABLE( stuff->drawable, client) ))
  1371.     {
  1372.     client->errorValue = stuff->drawable;
  1373.         return (BadDrawable);
  1374.     }
  1375.     len = stuff->length -  (sizeof(xCreateGCReq) >> 2);
  1376.     if (len != Ones(stuff->mask))
  1377.         return BadLength;
  1378.     pGC = (GC *)CreateGC(pDraw, stuff->mask, 
  1379.              (XID *) &stuff[1], &error);
  1380.     if (error != Success)
  1381.         return error;
  1382.     if (!AddResource(stuff->gc, RT_GC, (pointer)pGC))
  1383.     return (BadAlloc);
  1384.     return(client->noClientException);
  1385. }
  1386.  
  1387. int
  1388. ProcChangeGC(client)
  1389.     register ClientPtr client;
  1390. {
  1391.     GC *pGC;
  1392.     REQUEST(xChangeGCReq);
  1393.     int result;
  1394.     unsigned len;
  1395.         
  1396.     REQUEST_AT_LEAST_SIZE(xChangeGCReq);
  1397.     VERIFY_GC(pGC, stuff->gc, client);
  1398.     len = stuff->length -  (sizeof(xChangeGCReq) >> 2);
  1399.     if (len != Ones(stuff->mask))
  1400.         return BadLength;
  1401.     result = DoChangeGC(pGC, stuff->mask, (XID *) &stuff[1], 0);
  1402.     if (client->noClientException != Success)
  1403.         return(client->noClientException);
  1404.     else
  1405.     {
  1406.     client->errorValue = clientErrorValue;
  1407.         return(result);
  1408.     }
  1409. }
  1410.  
  1411. int
  1412. ProcCopyGC(client)
  1413.     register ClientPtr client;
  1414. {
  1415.     register GC *dstGC;
  1416.     register GC *pGC;
  1417.     int result;
  1418.     REQUEST(xCopyGCReq);
  1419.  
  1420.     REQUEST_SIZE_MATCH(xCopyGCReq);
  1421.     VERIFY_GC( pGC, stuff->srcGC, client);
  1422.     VERIFY_GC( dstGC, stuff->dstGC, client);
  1423.     if ((dstGC->pScreen != pGC->pScreen) || (dstGC->depth != pGC->depth))
  1424.         return (BadMatch);    
  1425.     result = CopyGC(pGC, dstGC, stuff->mask);
  1426.     if (client->noClientException != Success)
  1427.         return(client->noClientException);
  1428.     else
  1429.     {
  1430.     client->errorValue = clientErrorValue;
  1431.         return(result);
  1432.     }
  1433. }
  1434.  
  1435. int
  1436. ProcSetDashes(client)
  1437.     register ClientPtr client;
  1438. {
  1439.     register GC *pGC;
  1440.     int result;
  1441.     REQUEST(xSetDashesReq);
  1442.  
  1443.     REQUEST_FIXED_SIZE(xSetDashesReq, stuff->nDashes);
  1444.     if (stuff->nDashes == 0)
  1445.     {
  1446.      client->errorValue = 0;
  1447.          return BadValue;
  1448.     }
  1449.  
  1450.     VERIFY_GC(pGC,stuff->gc, client);
  1451.  
  1452.     result = SetDashes(pGC, stuff->dashOffset, stuff->nDashes,
  1453.                (unsigned char *)&stuff[1]);
  1454.     if (client->noClientException != Success)
  1455.         return(client->noClientException);
  1456.     else
  1457.     {
  1458.     client->errorValue = clientErrorValue;
  1459.         return(result);
  1460.     }
  1461. }
  1462.  
  1463. int
  1464. ProcSetClipRectangles(client)
  1465.     register ClientPtr client;
  1466. {
  1467.     int    nr;
  1468.     int result;
  1469.     register GC *pGC;
  1470.     REQUEST(xSetClipRectanglesReq);
  1471.  
  1472.     REQUEST_AT_LEAST_SIZE(xSetClipRectanglesReq);
  1473.     if ((stuff->ordering != Unsorted) && (stuff->ordering != YSorted) &&
  1474.     (stuff->ordering != YXSorted) && (stuff->ordering != YXBanded))
  1475.     {
  1476.     client->errorValue = stuff->ordering;
  1477.         return BadValue;
  1478.     }
  1479.     VERIFY_GC(pGC,stuff->gc, client);
  1480.          
  1481.     nr = (stuff->length << 2) - sizeof(xSetClipRectanglesReq);
  1482.     if (nr & 4)
  1483.     return(BadLength);
  1484.     nr >>= 3;
  1485.     result = SetClipRects(pGC, stuff->xOrigin, stuff->yOrigin,
  1486.               nr, (xRectangle *)&stuff[1], (int)stuff->ordering);
  1487.     if (client->noClientException != Success)
  1488.         return(client->noClientException);
  1489.     else
  1490.         return(result);
  1491. }
  1492.  
  1493. int
  1494. ProcFreeGC(client)
  1495.     register ClientPtr client;
  1496. {
  1497.     register GC *pGC;
  1498.     REQUEST(xResourceReq);
  1499.  
  1500.     REQUEST_SIZE_MATCH(xResourceReq);
  1501.     VERIFY_GC(pGC,stuff->id,client);
  1502.     FreeResource(stuff->id, RT_NONE);
  1503.     return(client->noClientException);
  1504. }
  1505.  
  1506. int
  1507. ProcClearToBackground(client)
  1508.     register ClientPtr client;
  1509. {
  1510.     REQUEST(xClearAreaReq);
  1511.     register WindowPtr pWin;
  1512.  
  1513.     REQUEST_SIZE_MATCH(xClearAreaReq);
  1514.     pWin = (WindowPtr)LookupWindow( stuff->window, client);
  1515.     if (!pWin)
  1516.         return(BadWindow);
  1517.     if (pWin->drawable.class == InputOnly)
  1518.     {
  1519.     client->errorValue = stuff->window;
  1520.     return (BadMatch);
  1521.     }            
  1522.     if ((stuff->exposures != xTrue) && (stuff->exposures != xFalse))
  1523.     {
  1524.     client->errorValue = stuff->exposures;
  1525.         return(BadValue);
  1526.     }
  1527.     (*pWin->drawable.pScreen->ClearToBackground)(pWin, stuff->x, stuff->y,
  1528.                    stuff->width, stuff->height,
  1529.                    (Bool)stuff->exposures);
  1530.     return(client->noClientException);
  1531. }
  1532.  
  1533. int
  1534. ProcCopyArea(client)
  1535.     register ClientPtr client;
  1536. {
  1537.     register DrawablePtr pDst;
  1538.     register DrawablePtr pSrc;
  1539.     register GC *pGC;
  1540.     REQUEST(xCopyAreaReq);
  1541.     RegionPtr pRgn;
  1542.  
  1543.     REQUEST_SIZE_MATCH(xCopyAreaReq);
  1544.  
  1545.     VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pDst, pGC, client); 
  1546.     if (stuff->dstDrawable != stuff->srcDrawable)
  1547.     {
  1548.         if (!(pSrc = LOOKUP_DRAWABLE(stuff->srcDrawable, client)))
  1549.     {
  1550.         client->errorValue = stuff->srcDrawable;
  1551.             return(BadDrawable);
  1552.     }
  1553.     if ((pDst->pScreen != pSrc->pScreen) || (pDst->depth != pSrc->depth))
  1554.     {
  1555.         client->errorValue = stuff->dstDrawable;
  1556.         return (BadMatch);
  1557.     }
  1558.     }
  1559.     else
  1560.         pSrc = pDst;
  1561.     pRgn = (*pGC->ops->CopyArea)(pSrc, pDst, pGC, stuff->srcX, stuff->srcY,
  1562.                  stuff->width, stuff->height, 
  1563.                  stuff->dstX, stuff->dstY);
  1564.     if (pGC->graphicsExposures)
  1565.     {
  1566.     (*pDst->pScreen->SendGraphicsExpose)
  1567.          (client, pRgn, stuff->dstDrawable, X_CopyArea, 0);
  1568.     if (pRgn)
  1569.         (*pDst->pScreen->RegionDestroy) (pRgn);
  1570.     }
  1571.  
  1572.     return(client->noClientException);
  1573. }
  1574.  
  1575. int
  1576. ProcCopyPlane(client)
  1577.     register ClientPtr client;
  1578. {
  1579.     register DrawablePtr psrcDraw, pdstDraw;
  1580.     register GC *pGC;
  1581.     REQUEST(xCopyPlaneReq);
  1582.     RegionPtr pRgn;
  1583.  
  1584.     REQUEST_SIZE_MATCH(xCopyPlaneReq);
  1585.  
  1586.     VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pdstDraw, pGC, client);
  1587.     if (stuff->dstDrawable != stuff->srcDrawable)
  1588.     {
  1589.         if (!(psrcDraw = LOOKUP_DRAWABLE(stuff->srcDrawable, client)))
  1590.     {
  1591.         client->errorValue = stuff->srcDrawable;
  1592.             return(BadDrawable);
  1593.     }
  1594.     if (pdstDraw->pScreen != psrcDraw->pScreen)
  1595.     {
  1596.         client->errorValue = stuff->dstDrawable;
  1597.         return (BadMatch);
  1598.     }
  1599.     }
  1600.     else
  1601.         psrcDraw = pdstDraw;
  1602.  
  1603.     /* Check to see if stuff->bitPlane has exactly ONE good bit set */
  1604.     if(stuff->bitPlane == 0 || (stuff->bitPlane & (stuff->bitPlane - 1)) ||
  1605.        (stuff->bitPlane > (1L << (psrcDraw->depth - 1))))
  1606.     {
  1607.        client->errorValue = stuff->bitPlane;
  1608.        return(BadValue);
  1609.     }
  1610.  
  1611.     pRgn = (*pGC->ops->CopyPlane)(psrcDraw, pdstDraw, pGC, stuff->srcX, stuff->srcY,
  1612.                  stuff->width, stuff->height, 
  1613.                  stuff->dstX, stuff->dstY, stuff->bitPlane);
  1614.     if (pGC->graphicsExposures)
  1615.     {
  1616.     (*pdstDraw->pScreen->SendGraphicsExpose)
  1617.          (client, pRgn, stuff->dstDrawable, X_CopyPlane, 0);
  1618.     if (pRgn)
  1619.         (*pdstDraw->pScreen->RegionDestroy) (pRgn);
  1620.     }
  1621.     return(client->noClientException);
  1622. }
  1623.  
  1624. int
  1625. ProcPolyPoint(client)
  1626.     register ClientPtr client;
  1627. {
  1628.     int npoint;
  1629.     register GC *pGC;
  1630.     register DrawablePtr pDraw;
  1631.     REQUEST(xPolyPointReq);
  1632.  
  1633.     REQUEST_AT_LEAST_SIZE(xPolyPointReq);
  1634.     if ((stuff->coordMode != CoordModeOrigin) && 
  1635.     (stuff->coordMode != CoordModePrevious))
  1636.     {
  1637.     client->errorValue = stuff->coordMode;
  1638.         return BadValue;
  1639.     }
  1640.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client); 
  1641.     npoint = ((stuff->length << 2) - sizeof(xPolyPointReq)) >> 2;
  1642.     if (npoint)
  1643.         (*pGC->ops->PolyPoint)(pDraw, pGC, stuff->coordMode, npoint,
  1644.               (xPoint *) &stuff[1]);
  1645.     return (client->noClientException);
  1646. }
  1647.  
  1648. int
  1649. ProcPolyLine(client)
  1650.     register ClientPtr client;
  1651. {
  1652.     int npoint;
  1653.     register GC *pGC;
  1654.     register DrawablePtr pDraw;
  1655.     REQUEST(xPolyLineReq);
  1656.  
  1657.     REQUEST_AT_LEAST_SIZE(xPolyLineReq);
  1658.     if ((stuff->coordMode != CoordModeOrigin) && 
  1659.     (stuff->coordMode != CoordModePrevious))
  1660.     {
  1661.     client->errorValue = stuff->coordMode;
  1662.         return BadValue;
  1663.     }
  1664.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1665.     npoint = ((stuff->length << 2) - sizeof(xPolyLineReq)) >> 2;
  1666.     if (npoint)
  1667.     (*pGC->ops->Polylines)(pDraw, pGC, stuff->coordMode, npoint, 
  1668.                   (xPoint *) &stuff[1]);
  1669.     return(client->noClientException);
  1670. }
  1671.  
  1672. int
  1673. ProcPolySegment(client)
  1674.     register ClientPtr client;
  1675. {
  1676.     int nsegs;
  1677.     register GC *pGC;
  1678.     register DrawablePtr pDraw;
  1679.     REQUEST(xPolySegmentReq);
  1680.  
  1681.     REQUEST_AT_LEAST_SIZE(xPolySegmentReq);
  1682.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1683.     nsegs = (stuff->length << 2) - sizeof(xPolySegmentReq);
  1684.     if (nsegs & 4)
  1685.     return(BadLength);
  1686.     nsegs >>= 3;
  1687.     if (nsegs)
  1688.         (*pGC->ops->PolySegment)(pDraw, pGC, nsegs, (xSegment *) &stuff[1]);
  1689.     return (client->noClientException);
  1690. }
  1691.  
  1692. int
  1693. ProcPolyRectangle (client)
  1694.     register ClientPtr client;
  1695. {
  1696.     int nrects;
  1697.     register GC *pGC;
  1698.     register DrawablePtr pDraw;
  1699.     REQUEST(xPolyRectangleReq);
  1700.  
  1701.     REQUEST_AT_LEAST_SIZE(xPolyRectangleReq);
  1702.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1703.     nrects = (stuff->length << 2) - sizeof(xPolyRectangleReq);
  1704.     if (nrects & 4)
  1705.     return(BadLength);
  1706.     nrects >>= 3;
  1707.     if (nrects)
  1708.         (*pGC->ops->PolyRectangle)(pDraw, pGC, 
  1709.             nrects, (xRectangle *) &stuff[1]);
  1710.     return(client->noClientException);
  1711. }
  1712.  
  1713. int
  1714. ProcPolyArc(client)
  1715.     register ClientPtr client;
  1716. {
  1717.     int        narcs;
  1718.     register GC *pGC;
  1719.     register DrawablePtr pDraw;
  1720.     REQUEST(xPolyArcReq);
  1721.  
  1722.     REQUEST_AT_LEAST_SIZE(xPolyArcReq);
  1723.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1724.     narcs = (stuff->length << 2) - sizeof(xPolyArcReq);
  1725.     if (narcs % sizeof(xArc))
  1726.     return(BadLength);
  1727.     narcs /= sizeof(xArc);
  1728.     if (narcs)
  1729.         (*pGC->ops->PolyArc)(pDraw, pGC, narcs, (xArc *) &stuff[1]);
  1730.     return (client->noClientException);
  1731. }
  1732.  
  1733. int
  1734. ProcFillPoly(client)
  1735.     register ClientPtr client;
  1736. {
  1737.     int          things;
  1738.     register GC *pGC;
  1739.     register DrawablePtr pDraw;
  1740.     REQUEST(xFillPolyReq);
  1741.  
  1742.     REQUEST_AT_LEAST_SIZE(xFillPolyReq);
  1743.     if ((stuff->shape != Complex) && (stuff->shape != Nonconvex) &&  
  1744.     (stuff->shape != Convex))
  1745.     {
  1746.     client->errorValue = stuff->shape;
  1747.         return BadValue;
  1748.     }
  1749.     if ((stuff->coordMode != CoordModeOrigin) && 
  1750.     (stuff->coordMode != CoordModePrevious))
  1751.     {
  1752.     client->errorValue = stuff->coordMode;
  1753.         return BadValue;
  1754.     }
  1755.  
  1756.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1757.     things = ((stuff->length << 2) - sizeof(xFillPolyReq)) >> 2;
  1758.     if (things)
  1759.         (*pGC->ops->FillPolygon) (pDraw, pGC, stuff->shape,
  1760.              stuff->coordMode, things,
  1761.              (DDXPointPtr) &stuff[1]);
  1762.     return(client->noClientException);
  1763. }
  1764.  
  1765. int
  1766. ProcPolyFillRectangle(client)
  1767.     register ClientPtr client;
  1768. {
  1769.     int             things;
  1770.     register GC *pGC;
  1771.     register DrawablePtr pDraw;
  1772.     REQUEST(xPolyFillRectangleReq);
  1773.  
  1774.     REQUEST_AT_LEAST_SIZE(xPolyFillRectangleReq);
  1775.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1776.     things = (stuff->length << 2) - sizeof(xPolyFillRectangleReq);
  1777.     if (things & 4)
  1778.     return(BadLength);
  1779.     things >>= 3;
  1780.     if (things)
  1781.         (*pGC->ops->PolyFillRect) (pDraw, pGC, things,
  1782.               (xRectangle *) &stuff[1]);
  1783.     return (client->noClientException);
  1784. }
  1785.  
  1786. int
  1787. ProcPolyFillArc               (client)
  1788.     register ClientPtr client;
  1789. {
  1790.     int        narcs;
  1791.     register GC *pGC;
  1792.     register DrawablePtr pDraw;
  1793.     REQUEST(xPolyFillArcReq);
  1794.  
  1795.     REQUEST_AT_LEAST_SIZE(xPolyFillArcReq);
  1796.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1797.     narcs = (stuff->length << 2) - sizeof(xPolyFillArcReq);
  1798.     if (narcs % sizeof(xArc))
  1799.     return(BadLength);
  1800.     narcs /= sizeof(xArc);
  1801.     if (narcs)
  1802.         (*pGC->ops->PolyFillArc) (pDraw, pGC, narcs, (xArc *) &stuff[1]);
  1803.     return (client->noClientException);
  1804. }
  1805.  
  1806. int
  1807. ProcPutImage(client)
  1808.     register ClientPtr client;
  1809. {
  1810.     register GC *pGC;
  1811.     register DrawablePtr pDraw;
  1812.     long length;
  1813.     REQUEST(xPutImageReq);
  1814.  
  1815.     REQUEST_AT_LEAST_SIZE(xPutImageReq);
  1816.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1817.     if (stuff->format == XYBitmap)
  1818.     {
  1819.         if ((stuff->depth != 1) ||
  1820.         (stuff->leftPad >= screenInfo.bitmapScanlinePad))
  1821.             return BadMatch;
  1822.         length = PixmapBytePad(stuff->width + stuff->leftPad, 1);
  1823.     }
  1824.     else if (stuff->format == XYPixmap)
  1825.     {
  1826.         if ((pDraw->depth != stuff->depth) || 
  1827.         (stuff->leftPad >= screenInfo.bitmapScanlinePad))
  1828.             return BadMatch;
  1829.         length = PixmapBytePad(stuff->width + stuff->leftPad, 1);
  1830.     length *= stuff->depth;
  1831.     }
  1832.     else if (stuff->format == ZPixmap)
  1833.     {
  1834.         if ((pDraw->depth != stuff->depth) || (stuff->leftPad != 0))
  1835.             return BadMatch;
  1836.         length = PixmapBytePad(stuff->width, stuff->depth);
  1837.     }
  1838.     else
  1839.     {
  1840.     client->errorValue = stuff->format;
  1841.         return BadValue;
  1842.     }
  1843.     length *= stuff->height;
  1844.     if ((((length + 3) >> 2) + (sizeof(xPutImageReq) >> 2)) != stuff->length)
  1845.     return BadLength;
  1846.     (*pGC->ops->PutImage) (pDraw, pGC, stuff->depth, stuff->dstX, stuff->dstY,
  1847.           stuff->width, stuff->height, 
  1848.           stuff->leftPad, stuff->format, 
  1849.           (char *) &stuff[1]);
  1850.      return (client->noClientException);
  1851. }
  1852.  
  1853. int
  1854. ProcGetImage(client)
  1855.     register ClientPtr    client;
  1856. {
  1857.     register DrawablePtr pDraw;
  1858.     int            nlines, linesPerBuf;
  1859.     register int    height, linesDone;
  1860.     long        widthBytesLine, length;
  1861.     Mask        plane;
  1862.     char        *pBuf;
  1863.     xGetImageReply    xgi;
  1864.  
  1865.     REQUEST(xGetImageReq);
  1866.  
  1867.     height = stuff->height;
  1868.     REQUEST_SIZE_MATCH(xGetImageReq);
  1869.     if ((stuff->format != XYPixmap) && (stuff->format != ZPixmap))
  1870.     {
  1871.     client->errorValue = stuff->format;
  1872.         return(BadValue);
  1873.     }
  1874.     if(!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client) ))
  1875.     {
  1876.     client->errorValue = stuff->drawable;
  1877.     return (BadDrawable);
  1878.     }
  1879.     if(pDraw->type == DRAWABLE_WINDOW)
  1880.     {
  1881.       if( /* check for being viewable */
  1882.      !((WindowPtr) pDraw)->realized ||
  1883.       /* check for being on screen */
  1884.          pDraw->x + stuff->x < 0 ||
  1885.       pDraw->x + stuff->x + (int)stuff->width > pDraw->pScreen->width ||
  1886.          pDraw->y + stuff->y < 0 ||
  1887.          pDraw->y + stuff->y + height > pDraw->pScreen->height ||
  1888.           /* check for being inside of border */
  1889.          stuff->x < - wBorderWidth((WindowPtr)pDraw) ||
  1890.          stuff->x + (int)stuff->width >
  1891.         wBorderWidth((WindowPtr)pDraw) + (int)pDraw->width ||
  1892.          stuff->y < -wBorderWidth((WindowPtr)pDraw) ||
  1893.          stuff->y + height >
  1894.         wBorderWidth ((WindowPtr)pDraw) + (int)pDraw->height
  1895.         )
  1896.         return(BadMatch);
  1897.     xgi.visual = wVisual (((WindowPtr) pDraw));
  1898.     }
  1899.     else
  1900.     {
  1901.       if(stuff->x < 0 ||
  1902.          stuff->x+(int)stuff->width > pDraw->width ||
  1903.          stuff->y < 0 ||
  1904.          stuff->y+height > pDraw->height
  1905.         )
  1906.         return(BadMatch);
  1907.     xgi.visual = None;
  1908.     }
  1909.     xgi.type = X_Reply;
  1910.     xgi.sequenceNumber = client->sequence;
  1911.     xgi.depth = pDraw->depth;
  1912.     if(stuff->format == ZPixmap)
  1913.     {
  1914.     widthBytesLine = PixmapBytePad(stuff->width, pDraw->depth);
  1915.     length = widthBytesLine * height;
  1916.     }
  1917.     else 
  1918.     {
  1919.     widthBytesLine = PixmapBytePad(stuff->width, 1);
  1920.     plane = ((Mask)1) << (pDraw->depth - 1);
  1921.     /* only planes asked for */
  1922.     length = widthBytesLine * height *
  1923.          Ones(stuff->planeMask & (plane | (plane - 1)));
  1924.     }
  1925.     xgi.length = (length + 3) >> 2;
  1926.     if (widthBytesLine == 0 || height == 0)
  1927.     linesPerBuf = 0;
  1928.     else if (widthBytesLine >= IMAGE_BUFSIZE)
  1929.     linesPerBuf = 1;
  1930.     else
  1931.     {
  1932.     linesPerBuf = IMAGE_BUFSIZE / widthBytesLine;
  1933.     if (linesPerBuf > height)
  1934.         linesPerBuf = height;
  1935.     }
  1936.     length = linesPerBuf * widthBytesLine;
  1937.     if (linesPerBuf < height)
  1938.     {
  1939.     /* we have to make sure intermediate buffers don't need padding */
  1940.     while ((linesPerBuf > 1) && (length & 3))
  1941.     {
  1942.         linesPerBuf--;
  1943.         length -= widthBytesLine;
  1944.     }
  1945.     while (length & 3)
  1946.     {
  1947.         linesPerBuf++;
  1948.         length += widthBytesLine;
  1949.     }
  1950.     }
  1951.     if(!(pBuf = (char *) ALLOCATE_LOCAL(length)))
  1952.         return (BadAlloc);
  1953.  
  1954.     WriteReplyToClient(client, sizeof (xGetImageReply), &xgi);
  1955.  
  1956.     if (linesPerBuf == 0)
  1957.     {
  1958.     /* nothing to do */
  1959.     }
  1960.     else if (stuff->format == ZPixmap)
  1961.     {
  1962.         linesDone = 0;
  1963.         while (height - linesDone > 0)
  1964.         {
  1965.         nlines = min(linesPerBuf, height - linesDone);
  1966.         (*pDraw->pScreen->GetImage) (pDraw,
  1967.                                      stuff->x,
  1968.                          stuff->y + linesDone,
  1969.                          stuff->width, 
  1970.                          nlines,
  1971.                          stuff->format,
  1972.                          stuff->planeMask,
  1973.                          pBuf);
  1974.         /* Note that this is NOT a call to WriteSwappedDataToClient,
  1975.                as we do NOT byte swap */
  1976.         (void)WriteToClient(client, (int)(nlines * widthBytesLine), pBuf);
  1977.         linesDone += nlines;
  1978.         }
  1979.     }
  1980.     else
  1981.     {
  1982.         for (; plane; plane >>= 1)
  1983.     {
  1984.         if (stuff->planeMask & plane)
  1985.         {
  1986.             linesDone = 0;
  1987.             while (height - linesDone > 0)
  1988.             {
  1989.             nlines = min(linesPerBuf, height - linesDone);
  1990.                 (*pDraw->pScreen->GetImage) (pDraw,
  1991.                                              stuff->x,
  1992.                                  stuff->y + linesDone,
  1993.                                  stuff->width, 
  1994.                                  nlines,
  1995.                                  stuff->format,
  1996.                                  plane,
  1997.                                  pBuf);
  1998.             /* Note: NOT a call to WriteSwappedDataToClient,
  1999.                as we do NOT byte swap */
  2000.             (void)WriteToClient(client, (int)(nlines * widthBytesLine),
  2001.                     pBuf);
  2002.             linesDone += nlines;
  2003.         }
  2004.             }
  2005.     }
  2006.     }
  2007.     DEALLOCATE_LOCAL(pBuf);
  2008.     return (client->noClientException);
  2009. }
  2010.  
  2011.  
  2012. int
  2013. ProcPolyText(client)
  2014.     register ClientPtr client;
  2015. {
  2016.     int        xorg;
  2017.     REQUEST(xPolyTextReq);
  2018.     register DrawablePtr pDraw;
  2019.     register GC *pGC;
  2020.     register FontPtr pFont;
  2021.  
  2022.     int (* polyText)();
  2023.     register unsigned char *pElt;
  2024.     unsigned char *pNextElt;
  2025.     unsigned char *endReq;
  2026.     int        itemSize;
  2027.     
  2028. #define TextEltHeader 2
  2029. #define FontShiftSize 5
  2030.  
  2031.     REQUEST_AT_LEAST_SIZE(xPolyTextReq);
  2032.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2033.  
  2034.     pElt = (unsigned char *)&stuff[1];
  2035.     endReq = ((unsigned char *) stuff) + (stuff->length <<2);
  2036.     xorg = stuff->x;
  2037.     if (stuff->reqType == X_PolyText8)
  2038.     {
  2039.     polyText = pGC->ops->PolyText8;
  2040.     itemSize = 1;
  2041.     }
  2042.     else
  2043.     {
  2044.     polyText =  pGC->ops->PolyText16;
  2045.     itemSize = 2;
  2046.     }
  2047.  
  2048.     while (endReq - pElt > TextEltHeader)
  2049.     {
  2050.     if (*pElt == FontChange)
  2051.         {
  2052.         Font    fid;
  2053.  
  2054.         if (endReq - pElt < FontShiftSize)
  2055.          return (BadLength);
  2056.         fid =  ((Font)*(pElt+4))        /* big-endian */
  2057.          | ((Font)*(pElt+3)) << 8
  2058.          | ((Font)*(pElt+2)) << 16
  2059.          | ((Font)*(pElt+1)) << 24;
  2060.         pFont = (FontPtr)LookupIDByType(fid, RT_FONT);
  2061.         if (!pFont)
  2062.         {
  2063.         client->errorValue = fid;
  2064.         return (BadFont);
  2065.         }
  2066.         if (pFont != pGC->font)
  2067.         {
  2068.         DoChangeGC( pGC, GCFont, &fid, 0);
  2069.         ValidateGC(pDraw, pGC);
  2070.         if (stuff->reqType == X_PolyText8)
  2071.             polyText = pGC->ops->PolyText8;
  2072.         else
  2073.             polyText = pGC->ops->PolyText16;
  2074.         }
  2075.         pElt += FontShiftSize;
  2076.     }
  2077.     else    /* print a string */
  2078.     {
  2079.         pNextElt = pElt + TextEltHeader + (*pElt)*itemSize;
  2080.         if ( pNextElt > endReq)
  2081.         return( BadLength);
  2082.         xorg += *((char *)(pElt + 1));    /* must be signed */
  2083.         xorg = (* polyText)(pDraw, pGC, xorg, stuff->y, *pElt,
  2084.         pElt + TextEltHeader);
  2085.         pElt = pNextElt;
  2086.     }
  2087.     }
  2088.     return (client->noClientException);
  2089. #undef TextEltHeader
  2090. #undef FontShiftSize
  2091. }
  2092.  
  2093. int
  2094. ProcImageText8(client)
  2095.     register ClientPtr client;
  2096. {
  2097.     register DrawablePtr pDraw;
  2098.     register GC *pGC;
  2099.  
  2100.     REQUEST(xImageTextReq);
  2101.  
  2102.     REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars);
  2103.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2104.  
  2105.     (*pGC->ops->ImageText8)(pDraw, pGC, stuff->x, stuff->y,
  2106.                stuff->nChars, &stuff[1]);
  2107.     return (client->noClientException);
  2108. }
  2109.  
  2110. int
  2111. ProcImageText16(client)
  2112.     register ClientPtr client;
  2113. {
  2114.     register DrawablePtr pDraw;
  2115.     register GC *pGC;
  2116.  
  2117.     REQUEST(xImageTextReq);
  2118.  
  2119.     REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars << 1);
  2120.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2121.  
  2122.     (*pGC->ops->ImageText16)(pDraw, pGC, stuff->x, stuff->y,
  2123.             stuff->nChars, &stuff[1]);
  2124.     return (client->noClientException);
  2125. }
  2126.  
  2127.  
  2128. int
  2129. ProcCreateColormap(client)
  2130.     register ClientPtr client;
  2131. {
  2132.     VisualPtr    pVisual;
  2133.     ColormapPtr    pmap;
  2134.     Colormap    mid;
  2135.     register WindowPtr   pWin;
  2136.     ScreenPtr pScreen;
  2137.     REQUEST(xCreateColormapReq);
  2138.     int i, result;
  2139.  
  2140.     REQUEST_SIZE_MATCH(xCreateColormapReq);
  2141.  
  2142.     if ((stuff->alloc != AllocNone) && (stuff->alloc != AllocAll))
  2143.     {
  2144.     client->errorValue = stuff->alloc;
  2145.         return(BadValue);
  2146.     }
  2147.     mid = stuff->mid;
  2148.     LEGAL_NEW_RESOURCE(mid, client);
  2149.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  2150.     if (!pWin)
  2151.         return(BadWindow);
  2152.  
  2153.     pScreen = pWin->drawable.pScreen;
  2154.     for (i = 0, pVisual = pScreen->visuals;
  2155.      i < pScreen->numVisuals;
  2156.      i++, pVisual++)
  2157.     {
  2158.     if (pVisual->vid != stuff->visual)
  2159.         continue;
  2160.     result =  CreateColormap(mid, pScreen, pVisual, &pmap,
  2161.                  (int)stuff->alloc, client->index);
  2162.     if (client->noClientException != Success)
  2163.         return(client->noClientException);
  2164.     else
  2165.         return(result);
  2166.     }
  2167.     client->errorValue = stuff->visual;
  2168.     return(BadValue);
  2169. }
  2170.  
  2171. int
  2172. ProcFreeColormap(client)
  2173.     register ClientPtr client;
  2174. {
  2175.     ColormapPtr pmap;
  2176.     REQUEST(xResourceReq);
  2177.  
  2178.     REQUEST_SIZE_MATCH(xResourceReq);
  2179.     pmap = (ColormapPtr )LookupIDByType(stuff->id, RT_COLORMAP);
  2180.     if (pmap) 
  2181.     {
  2182.     /* Freeing a default colormap is a no-op */
  2183.     if (!(pmap->flags & IsDefault))
  2184.         FreeResource(stuff->id, RT_NONE);
  2185.     return (client->noClientException);
  2186.     }
  2187.     else 
  2188.     {
  2189.     client->errorValue = stuff->id;
  2190.     return (BadColor);
  2191.     }
  2192. }
  2193.  
  2194.  
  2195. int
  2196. ProcCopyColormapAndFree(client)
  2197.     register ClientPtr client;
  2198. {
  2199.     Colormap    mid;
  2200.     ColormapPtr    pSrcMap;
  2201.     REQUEST(xCopyColormapAndFreeReq);
  2202.     int result;
  2203.  
  2204.     REQUEST_SIZE_MATCH(xCopyColormapAndFreeReq);
  2205.     mid = stuff->mid;
  2206.     LEGAL_NEW_RESOURCE(mid, client);
  2207.     if(pSrcMap = (ColormapPtr )LookupIDByType(stuff->srcCmap, RT_COLORMAP))
  2208.     {
  2209.     result = CopyColormapAndFree(mid, pSrcMap, client->index);
  2210.     if (client->noClientException != Success)
  2211.             return(client->noClientException);
  2212.     else
  2213.             return(result);
  2214.     }
  2215.     else
  2216.     {
  2217.     client->errorValue = stuff->srcCmap;
  2218.     return(BadColor);
  2219.     }
  2220. }
  2221.  
  2222. int
  2223. ProcInstallColormap(client)
  2224.     register ClientPtr client;
  2225. {
  2226.     ColormapPtr pcmp;
  2227.     REQUEST(xResourceReq);
  2228.  
  2229.     REQUEST_SIZE_MATCH(xResourceReq);
  2230.     pcmp = (ColormapPtr  )LookupIDByType(stuff->id, RT_COLORMAP);
  2231.     if (pcmp)
  2232.     {
  2233.         (*(pcmp->pScreen->InstallColormap)) (pcmp);
  2234.         return (client->noClientException);        
  2235.     }
  2236.     else
  2237.     {
  2238.         client->errorValue = stuff->id;
  2239.         return (BadColor);
  2240.     }
  2241. }
  2242.  
  2243. int
  2244. ProcUninstallColormap(client)
  2245.     register ClientPtr client;
  2246. {
  2247.     ColormapPtr pcmp;
  2248.     REQUEST(xResourceReq);
  2249.  
  2250.     REQUEST_SIZE_MATCH(xResourceReq);
  2251.     pcmp = (ColormapPtr )LookupIDByType(stuff->id, RT_COLORMAP);
  2252.     if (pcmp)
  2253.     {
  2254.     if(pcmp->mid != pcmp->pScreen->defColormap)
  2255.             (*(pcmp->pScreen->UninstallColormap)) (pcmp);
  2256.         return (client->noClientException);        
  2257.     }
  2258.     else
  2259.     {
  2260.         client->errorValue = stuff->id;
  2261.         return (BadColor);
  2262.     }
  2263. }
  2264.  
  2265. int
  2266. ProcListInstalledColormaps(client)
  2267.     register ClientPtr client;
  2268. {
  2269.     xListInstalledColormapsReply *preply; 
  2270.     int nummaps;
  2271.     WindowPtr pWin;
  2272.     REQUEST(xResourceReq);
  2273.  
  2274.     REQUEST_SIZE_MATCH(xResourceReq);
  2275.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  2276.  
  2277.     if (!pWin)
  2278.         return(BadWindow);
  2279.  
  2280.     preply = (xListInstalledColormapsReply *) 
  2281.         ALLOCATE_LOCAL(sizeof(xListInstalledColormapsReply) +
  2282.              pWin->drawable.pScreen->maxInstalledCmaps *
  2283.              sizeof(Colormap));
  2284.     if(!preply)
  2285.         return(BadAlloc);
  2286.  
  2287.     preply->type = X_Reply;
  2288.     preply->sequenceNumber = client->sequence;
  2289.     nummaps = (*pWin->drawable.pScreen->ListInstalledColormaps)
  2290.         (pWin->drawable.pScreen, (Colormap *)&preply[1]);
  2291.     preply->nColormaps = nummaps;
  2292.     preply->length = nummaps;
  2293.     WriteReplyToClient(client, sizeof (xListInstalledColormapsReply), preply);
  2294.     client->pSwapReplyFunc = Swap32Write;
  2295.     WriteSwappedDataToClient(client, nummaps * sizeof(Colormap), &preply[1]);
  2296.     DEALLOCATE_LOCAL(preply);
  2297.     return(client->noClientException);
  2298. }
  2299.  
  2300. int
  2301. ProcAllocColor                (client)
  2302.     register ClientPtr client;
  2303. {
  2304.     ColormapPtr pmap;
  2305.     int    retval;
  2306.     xAllocColorReply acr;
  2307.     REQUEST(xAllocColorReq);
  2308.  
  2309.     REQUEST_SIZE_MATCH(xAllocColorReq);
  2310.     pmap = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2311.     if (pmap)
  2312.     {
  2313.     acr.type = X_Reply;
  2314.     acr.length = 0;
  2315.     acr.sequenceNumber = client->sequence;
  2316.     acr.red = stuff->red;
  2317.     acr.green = stuff->green;
  2318.     acr.blue = stuff->blue;
  2319.     acr.pixel = 0;
  2320.     if(retval = AllocColor(pmap, &acr.red, &acr.green, &acr.blue,
  2321.                            &acr.pixel, client->index))
  2322.     {
  2323.             if (client->noClientException != Success)
  2324.                 return(client->noClientException);
  2325.         else
  2326.             return (retval);
  2327.     }
  2328.         WriteReplyToClient(client, sizeof(xAllocColorReply), &acr);
  2329.     return (client->noClientException);
  2330.  
  2331.     }
  2332.     else
  2333.     {
  2334.         client->errorValue = stuff->cmap;
  2335.         return (BadColor);
  2336.     }
  2337. }
  2338.  
  2339. int
  2340. ProcAllocNamedColor           (client)
  2341.     register ClientPtr client;
  2342. {
  2343.     ColormapPtr pcmp;
  2344.     REQUEST(xAllocNamedColorReq);
  2345.  
  2346.     REQUEST_FIXED_SIZE(xAllocNamedColorReq, stuff->nbytes);
  2347.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2348.     if (pcmp)
  2349.     {
  2350.     int        retval;
  2351.  
  2352.     xAllocNamedColorReply ancr;
  2353.  
  2354.     ancr.type = X_Reply;
  2355.     ancr.length = 0;
  2356.     ancr.sequenceNumber = client->sequence;
  2357.  
  2358.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2359.                      &ancr.exactRed, &ancr.exactGreen, &ancr.exactBlue))
  2360.     {
  2361.         ancr.screenRed = ancr.exactRed;
  2362.         ancr.screenGreen = ancr.exactGreen;
  2363.         ancr.screenBlue = ancr.exactBlue;
  2364.         ancr.pixel = 0;
  2365.         if(retval = AllocColor(pcmp,
  2366.                      &ancr.screenRed, &ancr.screenGreen, &ancr.screenBlue,
  2367.              &ancr.pixel, client->index))
  2368.         {
  2369.                 if (client->noClientException != Success)
  2370.                     return(client->noClientException);
  2371.                 else
  2372.                     return(retval);
  2373.         }
  2374.             WriteReplyToClient(client, sizeof (xAllocNamedColorReply), &ancr);
  2375.         return (client->noClientException);
  2376.     }
  2377.     else
  2378.         return(BadName);
  2379.     
  2380.     }
  2381.     else
  2382.     {
  2383.         client->errorValue = stuff->cmap;
  2384.         return (BadColor);
  2385.     }
  2386. }
  2387.  
  2388. int
  2389. ProcAllocColorCells           (client)
  2390.     register ClientPtr client;
  2391. {
  2392.     ColormapPtr pcmp;
  2393.     REQUEST(xAllocColorCellsReq);
  2394.  
  2395.     REQUEST_SIZE_MATCH(xAllocColorCellsReq);
  2396.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2397.     if (pcmp)
  2398.     {
  2399.     xAllocColorCellsReply    accr;
  2400.     int            npixels, nmasks, retval;
  2401.     long            length;
  2402.     unsigned long        *ppixels, *pmasks;
  2403.  
  2404.     npixels = stuff->colors;
  2405.     if (!npixels)
  2406.     {
  2407.         client->errorValue = npixels;
  2408.         return (BadValue);
  2409.     }
  2410.     nmasks = stuff->planes;
  2411.     length = ((long)npixels + (long)nmasks) * sizeof(Pixel);
  2412.     ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2413.     if(!ppixels)
  2414.             return(BadAlloc);
  2415.     pmasks = ppixels + npixels;
  2416.  
  2417.     if(retval = AllocColorCells(client->index, pcmp, npixels, nmasks, 
  2418.                     (Bool)stuff->contiguous, ppixels, pmasks))
  2419.     {
  2420.         DEALLOCATE_LOCAL(ppixels);
  2421.             if (client->noClientException != Success)
  2422.                 return(client->noClientException);
  2423.         else
  2424.             return(retval);
  2425.     }
  2426.     accr.type = X_Reply;
  2427.     accr.length = length >> 2;
  2428.     accr.sequenceNumber = client->sequence;
  2429.     accr.nPixels = npixels;
  2430.     accr.nMasks = nmasks;
  2431.         WriteReplyToClient(client, sizeof (xAllocColorCellsReply), &accr);
  2432.     client->pSwapReplyFunc = Swap32Write;
  2433.     WriteSwappedDataToClient(client, length, ppixels);
  2434.     DEALLOCATE_LOCAL(ppixels);
  2435.         return (client->noClientException);        
  2436.     }
  2437.     else
  2438.     {
  2439.         client->errorValue = stuff->cmap;
  2440.         return (BadColor);
  2441.     }
  2442. }
  2443.  
  2444. int
  2445. ProcAllocColorPlanes(client)
  2446.     register ClientPtr client;
  2447. {
  2448.     ColormapPtr pcmp;
  2449.     REQUEST(xAllocColorPlanesReq);
  2450.  
  2451.     REQUEST_SIZE_MATCH(xAllocColorPlanesReq);
  2452.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2453.     if (pcmp)
  2454.     {
  2455.     xAllocColorPlanesReply    acpr;
  2456.     int            npixels, retval;
  2457.     long            length;
  2458.     unsigned long        *ppixels;
  2459.  
  2460.     npixels = stuff->colors;
  2461.     if (!npixels)
  2462.     {
  2463.         client->errorValue = npixels;
  2464.         return (BadValue);
  2465.     }
  2466.     acpr.type = X_Reply;
  2467.     acpr.sequenceNumber = client->sequence;
  2468.     acpr.nPixels = npixels;
  2469.     length = (long)npixels * sizeof(Pixel);
  2470.     ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2471.     if(!ppixels)
  2472.             return(BadAlloc);
  2473.     if(retval = AllocColorPlanes(client->index, pcmp, npixels,
  2474.         (int)stuff->red, (int)stuff->green, (int)stuff->blue,
  2475.         (int)stuff->contiguous, ppixels,
  2476.         &acpr.redMask, &acpr.greenMask, &acpr.blueMask))
  2477.     {
  2478.             DEALLOCATE_LOCAL(ppixels);
  2479.             if (client->noClientException != Success)
  2480.                 return(client->noClientException);
  2481.         else
  2482.             return(retval);
  2483.     }
  2484.     acpr.length = length >> 2;
  2485.     WriteReplyToClient(client, sizeof(xAllocColorPlanesReply), &acpr);
  2486.     client->pSwapReplyFunc = Swap32Write;
  2487.     WriteSwappedDataToClient(client, length, ppixels);
  2488.     DEALLOCATE_LOCAL(ppixels);
  2489.         return (client->noClientException);        
  2490.     }
  2491.     else
  2492.     {
  2493.         client->errorValue = stuff->cmap;
  2494.         return (BadColor);
  2495.     }
  2496. }
  2497.  
  2498. int
  2499. ProcFreeColors          (client)
  2500.     register ClientPtr client;
  2501. {
  2502.     ColormapPtr pcmp;
  2503.     REQUEST(xFreeColorsReq);
  2504.  
  2505.     REQUEST_AT_LEAST_SIZE(xFreeColorsReq);
  2506.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2507.     if (pcmp)
  2508.     {
  2509.     int    count;
  2510.         int     retval;
  2511.  
  2512.     if(pcmp->flags & AllAllocated)
  2513.         return(BadAccess);
  2514.     count = ((stuff->length << 2)- sizeof(xFreeColorsReq)) >> 2;
  2515.     retval =  FreeColors(pcmp, client->index, count,
  2516.         (unsigned long *)&stuff[1], stuff->planeMask);
  2517.         if (client->noClientException != Success)
  2518.             return(client->noClientException);
  2519.         else
  2520.     {
  2521.         client->errorValue = clientErrorValue;
  2522.             return(retval);
  2523.     }
  2524.  
  2525.     }
  2526.     else
  2527.     {
  2528.         client->errorValue = stuff->cmap;
  2529.         return (BadColor);
  2530.     }
  2531. }
  2532.  
  2533. int
  2534. ProcStoreColors               (client)
  2535.     register ClientPtr client;
  2536. {
  2537.     ColormapPtr pcmp;
  2538.     REQUEST(xStoreColorsReq);
  2539.  
  2540.     REQUEST_AT_LEAST_SIZE(xStoreColorsReq);
  2541.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2542.     if (pcmp)
  2543.     {
  2544.     int    count;
  2545.         int     retval;
  2546.  
  2547.         count = (stuff->length << 2) - sizeof(xStoreColorsReq);
  2548.     if (count % sizeof(xColorItem))
  2549.         return(BadLength);
  2550.     count /= sizeof(xColorItem);
  2551.     retval = StoreColors(pcmp, count, (xColorItem *)&stuff[1]);
  2552.         if (client->noClientException != Success)
  2553.             return(client->noClientException);
  2554.         else
  2555.     {
  2556.         client->errorValue = clientErrorValue;
  2557.             return(retval);
  2558.     }
  2559.     }
  2560.     else
  2561.     {
  2562.         client->errorValue = stuff->cmap;
  2563.         return (BadColor);
  2564.     }
  2565. }
  2566.  
  2567. int
  2568. ProcStoreNamedColor           (client)
  2569.     register ClientPtr client;
  2570. {
  2571.     ColormapPtr pcmp;
  2572.     REQUEST(xStoreNamedColorReq);
  2573.  
  2574.     REQUEST_FIXED_SIZE(xStoreNamedColorReq, stuff->nbytes);
  2575.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2576.     if (pcmp)
  2577.     {
  2578.     xColorItem    def;
  2579.         int             retval;
  2580.  
  2581.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1],
  2582.                      stuff->nbytes, &def.red, &def.green, &def.blue))
  2583.     {
  2584.         def.flags = stuff->flags;
  2585.         def.pixel = stuff->pixel;
  2586.         retval = StoreColors(pcmp, 1, &def);
  2587.             if (client->noClientException != Success)
  2588.                 return(client->noClientException);
  2589.         else
  2590.         return(retval);
  2591.     }
  2592.         return (BadName);        
  2593.     }
  2594.     else
  2595.     {
  2596.         client->errorValue = stuff->cmap;
  2597.         return (BadColor);
  2598.     }
  2599. }
  2600.  
  2601. int
  2602. ProcQueryColors(client)
  2603.     register ClientPtr client;
  2604. {
  2605.     ColormapPtr pcmp;
  2606.     REQUEST(xQueryColorsReq);
  2607.  
  2608.     REQUEST_AT_LEAST_SIZE(xQueryColorsReq);
  2609.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2610.     if (pcmp)
  2611.     {
  2612.     int            count, retval;
  2613.     xrgb             *prgbs;
  2614.     xQueryColorsReply    qcr;
  2615.  
  2616.     count = ((stuff->length << 2) - sizeof(xQueryColorsReq)) >> 2;
  2617.     prgbs = (xrgb *)ALLOCATE_LOCAL(count * sizeof(xrgb));
  2618.     if(!prgbs && count)
  2619.             return(BadAlloc);
  2620.     if(retval = QueryColors(pcmp, count, (unsigned long *)&stuff[1], prgbs))
  2621.     {
  2622.            if (prgbs) DEALLOCATE_LOCAL(prgbs);
  2623.         if (client->noClientException != Success)
  2624.                 return(client->noClientException);
  2625.         else
  2626.         {
  2627.         client->errorValue = clientErrorValue;
  2628.             return (retval);
  2629.         }
  2630.     }
  2631.     qcr.type = X_Reply;
  2632.     qcr.length = (count * sizeof(xrgb)) >> 2;
  2633.     qcr.sequenceNumber = client->sequence;
  2634.     qcr.nColors = count;
  2635.     WriteReplyToClient(client, sizeof(xQueryColorsReply), &qcr);
  2636.     if (count)
  2637.     {
  2638.         client->pSwapReplyFunc = SQColorsExtend;
  2639.         WriteSwappedDataToClient(client, count * sizeof(xrgb), prgbs);
  2640.     }
  2641.     if (prgbs) DEALLOCATE_LOCAL(prgbs);
  2642.     return(client->noClientException);
  2643.     
  2644.     }
  2645.     else
  2646.     {
  2647.         client->errorValue = stuff->cmap;
  2648.         return (BadColor);
  2649.     }
  2650.  
  2651. int
  2652. ProcLookupColor(client)
  2653.     register ClientPtr client;
  2654. {
  2655.     ColormapPtr pcmp;
  2656.     REQUEST(xLookupColorReq);
  2657.  
  2658.     REQUEST_FIXED_SIZE(xLookupColorReq, stuff->nbytes);
  2659.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2660.     if (pcmp)
  2661.     {
  2662.     xLookupColorReply lcr;
  2663.  
  2664.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2665.                      &lcr.exactRed, &lcr.exactGreen, &lcr.exactBlue))
  2666.     {
  2667.         lcr.type = X_Reply;
  2668.         lcr.length = 0;
  2669.         lcr.sequenceNumber = client->sequence;
  2670.         lcr.screenRed = lcr.exactRed;
  2671.         lcr.screenGreen = lcr.exactGreen;
  2672.         lcr.screenBlue = lcr.exactBlue;
  2673.         (*pcmp->pScreen->ResolveColor)(&lcr.screenRed,
  2674.                                        &lcr.screenGreen,
  2675.                        &lcr.screenBlue,
  2676.                        pcmp->pVisual);
  2677.         WriteReplyToClient(client, sizeof(xLookupColorReply), &lcr);
  2678.         return(client->noClientException);
  2679.     }
  2680.         return (BadName);        
  2681.     }
  2682.     else
  2683.     {
  2684.         client->errorValue = stuff->cmap;
  2685.         return (BadColor);
  2686.     }
  2687. }
  2688.  
  2689. int
  2690. ProcCreateCursor( client)
  2691.     register ClientPtr client;
  2692. {
  2693.     CursorPtr    pCursor;
  2694.  
  2695.     register PixmapPtr     src;
  2696.     register PixmapPtr     msk;
  2697.     unsigned char *    srcbits;
  2698.     unsigned char *    mskbits;
  2699.     unsigned short    width, height;
  2700.     long        n;
  2701.     CursorMetricRec cm;
  2702.  
  2703.  
  2704.     REQUEST(xCreateCursorReq);
  2705.  
  2706.     REQUEST_SIZE_MATCH(xCreateCursorReq);
  2707.     LEGAL_NEW_RESOURCE(stuff->cid, client);
  2708.  
  2709.     src = (PixmapPtr)LookupIDByType(stuff->source, RT_PIXMAP);
  2710.     msk = (PixmapPtr)LookupIDByType(stuff->mask, RT_PIXMAP);
  2711.     if (   src == (PixmapPtr)NULL)
  2712.     {
  2713.     client->errorValue = stuff->source;
  2714.     return (BadPixmap);
  2715.     }
  2716.     if ( msk == (PixmapPtr)NULL)
  2717.     {
  2718.     if (stuff->mask != None)
  2719.     {
  2720.         client->errorValue = stuff->mask;
  2721.         return (BadPixmap);
  2722.     }
  2723.     }
  2724.     else if (  src->drawable.width != msk->drawable.width
  2725.         || src->drawable.height != msk->drawable.height
  2726.         || src->drawable.depth != 1
  2727.         || msk->drawable.depth != 1)
  2728.     return (BadMatch);
  2729.  
  2730.     width = src->drawable.width;
  2731.     height = src->drawable.height;
  2732.  
  2733.     if ( stuff->x > width 
  2734.       || stuff->y > height )
  2735.     return (BadMatch);
  2736.  
  2737.     n = PixmapBytePad(width, 1)*height;
  2738.     srcbits = (unsigned char *)xalloc(n);
  2739.     if (!srcbits)
  2740.     return (BadAlloc);
  2741.     mskbits = (unsigned char *)xalloc(n);
  2742.     if (!mskbits)
  2743.     {
  2744.     xfree(srcbits);
  2745.     return (BadAlloc);
  2746.     }
  2747.  
  2748.     (* src->drawable.pScreen->GetImage)( src, 0, 0, width, height,
  2749.                      XYPixmap, 1, srcbits);
  2750.     if ( msk == (PixmapPtr)NULL)
  2751.     {
  2752.     register unsigned char *bits = mskbits;
  2753.     while (--n >= 0)
  2754.         *bits++ = ~0;
  2755.     }
  2756.     else
  2757.     (* msk->drawable.pScreen->GetImage)( msk, 0, 0, width, height,
  2758.                          XYPixmap, 1, mskbits);
  2759.     cm.width = width;
  2760.     cm.height = height;
  2761.     cm.xhot = stuff->x;
  2762.     cm.yhot = stuff->y;
  2763.     pCursor = AllocCursor( srcbits, mskbits, &cm,
  2764.         stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2765.         stuff->backRed, stuff->backGreen, stuff->backBlue);
  2766.  
  2767.     if (pCursor && AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  2768.         return (client->noClientException);
  2769.     return BadAlloc;
  2770. }
  2771.  
  2772. int
  2773. ProcCreateGlyphCursor( client)
  2774.     register ClientPtr client;
  2775. {
  2776.     CursorPtr pCursor;
  2777.     int res;
  2778.  
  2779.     REQUEST(xCreateGlyphCursorReq);
  2780.  
  2781.     REQUEST_SIZE_MATCH(xCreateGlyphCursorReq);
  2782.     LEGAL_NEW_RESOURCE(stuff->cid, client);
  2783.  
  2784.     res = AllocGlyphCursor(stuff->source, stuff->sourceChar,
  2785.                stuff->mask, stuff->maskChar,
  2786.                stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2787.                stuff->backRed, stuff->backGreen, stuff->backBlue,
  2788.                &pCursor, client);
  2789.     if (res != Success)
  2790.     return res;
  2791.     if (AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  2792.     return client->noClientException;
  2793.     return BadAlloc;
  2794. }
  2795.  
  2796.  
  2797. int
  2798. ProcFreeCursor(client)
  2799.     register ClientPtr client;
  2800. {
  2801.     CursorPtr pCursor;
  2802.     REQUEST(xResourceReq);
  2803.  
  2804.     REQUEST_SIZE_MATCH(xResourceReq);
  2805.     pCursor = (CursorPtr)LookupIDByType(stuff->id, RT_CURSOR);
  2806.     if (pCursor) 
  2807.     {
  2808.     FreeResource(stuff->id, RT_NONE);
  2809.     return (client->noClientException);
  2810.     }
  2811.     else 
  2812.     {
  2813.     client->errorValue = stuff->id;
  2814.     return (BadCursor);
  2815.     }
  2816. }
  2817.  
  2818. int
  2819. ProcQueryBestSize   (client)
  2820.     register ClientPtr client;
  2821. {
  2822.     xQueryBestSizeReply    reply;
  2823.     register DrawablePtr pDraw;
  2824.     ScreenPtr pScreen;
  2825.     REQUEST(xQueryBestSizeReq);
  2826.  
  2827.     REQUEST_SIZE_MATCH(xQueryBestSizeReq);
  2828.     if ((stuff->class != CursorShape) && 
  2829.     (stuff->class != TileShape) && 
  2830.     (stuff->class != StippleShape))
  2831.     {
  2832.     client->errorValue = stuff->class;
  2833.         return(BadValue);
  2834.     }
  2835.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client)))
  2836.     {
  2837.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->drawable, client))) 
  2838.     {
  2839.         client->errorValue = stuff->drawable;
  2840.         return (BadDrawable);
  2841.     }
  2842.     if (stuff->class != CursorShape)
  2843.         return (BadMatch);
  2844.     }
  2845.     pScreen = pDraw->pScreen;
  2846.     (* pScreen->QueryBestSize)(stuff->class, &stuff->width,
  2847.                    &stuff->height);
  2848.     reply.type = X_Reply;
  2849.     reply.length = 0;
  2850.     reply.sequenceNumber = client->sequence;
  2851.     reply.width = stuff->width;
  2852.     reply.height = stuff->height;
  2853.     WriteReplyToClient(client, sizeof(xQueryBestSizeReply), &reply);
  2854.     return (client->noClientException);
  2855. }
  2856.  
  2857.  
  2858. int
  2859. ProcSetScreenSaver            (client)
  2860.     register ClientPtr client;
  2861. {
  2862.     int blankingOption, exposureOption;
  2863.     REQUEST(xSetScreenSaverReq);
  2864.  
  2865.     REQUEST_SIZE_MATCH(xSetScreenSaverReq);
  2866.     blankingOption = stuff->preferBlank;
  2867.     if ((blankingOption != DontPreferBlanking) &&
  2868.         (blankingOption != PreferBlanking) &&
  2869.         (blankingOption != DefaultBlanking))
  2870.         return BadMatch;
  2871.  
  2872.     exposureOption = stuff->allowExpose;
  2873.     if ((exposureOption != DontAllowExposures) &&
  2874.         (exposureOption != AllowExposures) &&
  2875.         (exposureOption != DefaultExposures))
  2876.         return BadMatch;
  2877.  
  2878.     if ((stuff->timeout < -1) || (stuff->interval < -1))
  2879.         return BadMatch;
  2880.  
  2881.     if (blankingOption == DefaultBlanking)
  2882.     ScreenSaverBlanking = defaultScreenSaverBlanking;
  2883.     else
  2884.     ScreenSaverBlanking = blankingOption; 
  2885.     if (exposureOption == DefaultExposures)
  2886.     ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
  2887.     else
  2888.     ScreenSaverAllowExposures = exposureOption;
  2889.  
  2890.     if (stuff->timeout >= 0)
  2891.     ScreenSaverTime = stuff->timeout * MILLI_PER_SECOND;
  2892.     else 
  2893.     ScreenSaverTime = defaultScreenSaverTime;
  2894.     if (stuff->interval >= 0)
  2895.     ScreenSaverInterval = stuff->interval * MILLI_PER_SECOND;
  2896.     else
  2897.     ScreenSaverInterval = defaultScreenSaverInterval;
  2898.     return (client->noClientException);
  2899. }
  2900.  
  2901. int
  2902. ProcGetScreenSaver(client)
  2903.     register ClientPtr client;
  2904. {
  2905.     xGetScreenSaverReply rep;
  2906.  
  2907.     rep.type = X_Reply;
  2908.     rep.length = 0;
  2909.     rep.sequenceNumber = client->sequence;
  2910.     rep.timeout = ScreenSaverTime / MILLI_PER_SECOND;
  2911.     rep.interval = ScreenSaverInterval / MILLI_PER_SECOND;
  2912.     rep.preferBlanking = ScreenSaverBlanking;
  2913.     rep.allowExposures = ScreenSaverAllowExposures;
  2914.     WriteReplyToClient(client, sizeof(xGetScreenSaverReply), &rep);
  2915.     return (client->noClientException);
  2916. }
  2917.  
  2918. int
  2919. ProcChangeHosts(client)
  2920.     register ClientPtr client;
  2921. {
  2922.     REQUEST(xChangeHostsReq);
  2923.     int result;
  2924.  
  2925.     REQUEST_FIXED_SIZE(xChangeHostsReq, stuff->hostLength);
  2926.  
  2927.     if(stuff->mode == HostInsert)
  2928.     result = AddHost(client, (int)stuff->hostFamily,
  2929.              stuff->hostLength, (pointer)&stuff[1]);
  2930.     else if (stuff->mode == HostDelete)
  2931.     result = RemoveHost(client, (int)stuff->hostFamily, 
  2932.                 stuff->hostLength, (pointer)&stuff[1]);  
  2933.     else
  2934.     {
  2935.     client->errorValue = stuff->mode;
  2936.         return BadValue;
  2937.     }
  2938.     if (!result)
  2939.     result = client->noClientException;
  2940.     return (result);
  2941. }
  2942.  
  2943. int
  2944. ProcListHosts(client)
  2945.     register ClientPtr client;
  2946. {
  2947. extern int GetHosts();
  2948.     xListHostsReply reply;
  2949.     int    len, nHosts, result;
  2950.     pointer    pdata;
  2951.     REQUEST(xListHostsReq);
  2952.  
  2953.     REQUEST_SIZE_MATCH(xListHostsReq);
  2954.     result = GetHosts(&pdata, &nHosts, &len, &reply.enabled);
  2955.     if (result != Success)
  2956.     return(result);
  2957.     reply.type = X_Reply;
  2958.     reply.sequenceNumber = client->sequence;
  2959.     reply.nHosts = nHosts;
  2960.     reply.length = len >> 2;
  2961.     WriteReplyToClient(client, sizeof(xListHostsReply), &reply);
  2962.     if (nHosts)
  2963.     {
  2964.     client->pSwapReplyFunc = SLHostsExtend;
  2965.     WriteSwappedDataToClient(client, len, pdata);
  2966.     }
  2967.     xfree(pdata);
  2968.     return (client->noClientException);
  2969. }
  2970.  
  2971. int
  2972. ProcChangeAccessControl(client)
  2973.     register ClientPtr client;
  2974. {
  2975.     int result;
  2976.     REQUEST(xSetAccessControlReq);
  2977.  
  2978.     REQUEST_SIZE_MATCH(xSetAccessControlReq);
  2979.     if ((stuff->mode != EnableAccess) && (stuff->mode != DisableAccess))
  2980.     {
  2981.     client->errorValue = stuff->mode;
  2982.         return BadValue;
  2983.     }
  2984.     result = ChangeAccessControl(client, stuff->mode == EnableAccess);
  2985.     if (!result)
  2986.     result = client->noClientException;
  2987.     return (result);
  2988. }
  2989.  
  2990. int
  2991. ProcKillClient(client)
  2992.     register ClientPtr client;
  2993. {
  2994.     REQUEST(xResourceReq);
  2995.  
  2996.     pointer *pResource;
  2997.     int clientIndex, myIndex;
  2998.  
  2999.     REQUEST_SIZE_MATCH(xResourceReq);
  3000.     if (stuff->id == AllTemporary)
  3001.     {
  3002.     CloseDownRetainedResources();
  3003.         return (client->noClientException);
  3004.     }
  3005.     pResource = (pointer *)LookupIDByClass(stuff->id, RC_ANY);
  3006.   
  3007.     clientIndex = CLIENT_ID(stuff->id);
  3008.  
  3009.     if (clientIndex && pResource && clients[clientIndex] &&
  3010.     (clients[clientIndex]->requestVector != InitialVector))
  3011.     {
  3012.     myIndex = client->index;
  3013.     CloseDownClient(clients[clientIndex]);
  3014.     if (myIndex == clientIndex)
  3015.     {
  3016.         /* force yield and return Success, so that Dispatch()
  3017.          * doesn't try to touch client
  3018.          */
  3019.         isItTimeToYield = TRUE;
  3020.         return (Success);
  3021.     }
  3022.     return (client->noClientException);
  3023.     }
  3024.     else
  3025.     {
  3026.     client->errorValue = stuff->id;
  3027.     return (BadValue);
  3028.     }
  3029. }
  3030.  
  3031. int
  3032. ProcSetFontPath(client)
  3033.     register ClientPtr client;
  3034. {
  3035.     unsigned char *ptr;
  3036.     unsigned long nbytes, total;
  3037.     long nfonts;
  3038.     int n, result;
  3039.     REQUEST(xSetFontPathReq);
  3040.     
  3041.     REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
  3042.     
  3043.     nbytes = (stuff->length << 2) - sizeof(xSetFontPathReq);
  3044.     total = nbytes;
  3045.     ptr = (unsigned char *)&stuff[1];
  3046.     nfonts = stuff->nFonts;
  3047.     while (--nfonts >= 0)
  3048.     {
  3049.     if ((total == 0) || (total < (n = (*ptr + 1))))
  3050.         return(BadLength);
  3051.     total -= n;
  3052.     ptr += n;
  3053.     }
  3054.     if (total >= 4)
  3055.     return(BadLength);
  3056.     result = SetFontPath(client, stuff->nFonts, (char *)&stuff[1]);
  3057.     if (!result)
  3058.     result = client->noClientException;
  3059.     return (result);
  3060. }
  3061.  
  3062. int
  3063. ProcGetFontPath(client)
  3064.     register ClientPtr client;
  3065. {
  3066.     FontPathPtr pFP;
  3067.     xGetFontPathReply reply;
  3068.     int stringLens, i;
  3069.     char *bufferStart;
  3070.     register char  *bufptr;
  3071.     REQUEST (xReq);
  3072.  
  3073.     REQUEST_SIZE_MATCH(xReq);
  3074.     pFP = GetFontPath();
  3075.     if (!pFP)
  3076.     return(BadAlloc);
  3077.     stringLens = 0;
  3078.     for (i=0; i<pFP->npaths; i++)
  3079.         stringLens += pFP->length[i];
  3080.  
  3081.     reply.type = X_Reply;
  3082.     reply.sequenceNumber = client->sequence;
  3083.     reply.length = (stringLens + pFP->npaths + 3) >> 2;
  3084.     reply.nPaths = pFP->npaths;
  3085.  
  3086.     bufptr = bufferStart = (char *)ALLOCATE_LOCAL(reply.length << 2);
  3087.     if(!bufptr && reply.length)
  3088.         return(BadAlloc);
  3089.             /* since WriteToClient long word aligns things, 
  3090.            copy to temp buffer and write all at once */
  3091.     for (i=0; i<pFP->npaths; i++)
  3092.     {
  3093.         *bufptr++ = pFP->length[i];
  3094.         bcopy(pFP->paths[i], bufptr,  pFP->length[i]);
  3095.         bufptr += pFP->length[i];
  3096.     }
  3097.     WriteReplyToClient(client, sizeof(xGetFontPathReply), &reply);
  3098.     if (stringLens || pFP->npaths)
  3099.     (void)WriteToClient(client, stringLens + pFP->npaths, bufferStart);
  3100.     if (bufferStart) DEALLOCATE_LOCAL(bufferStart);
  3101.     return(client->noClientException);
  3102. }
  3103.  
  3104. int
  3105. ProcChangeCloseDownMode(client)
  3106.     register ClientPtr client;
  3107. {
  3108.     REQUEST(xSetCloseDownModeReq);
  3109.  
  3110.     REQUEST_SIZE_MATCH(xSetCloseDownModeReq);
  3111.     if ((stuff->mode == AllTemporary) ||
  3112.     (stuff->mode == RetainPermanent) ||
  3113.     (stuff->mode == RetainTemporary))
  3114.     {
  3115.     client->closeDownMode = stuff->mode;
  3116.     return (client->noClientException);
  3117.     }
  3118.     else   
  3119.     {
  3120.     client->errorValue = stuff->mode;
  3121.     return (BadValue);
  3122.     }
  3123. }
  3124.  
  3125. int ProcForceScreenSaver(client)
  3126.     register ClientPtr client;
  3127. {    
  3128.     REQUEST(xForceScreenSaverReq);
  3129.  
  3130.     REQUEST_SIZE_MATCH(xForceScreenSaverReq);
  3131.     
  3132.     if ((stuff->mode != ScreenSaverReset) && 
  3133.     (stuff->mode != ScreenSaverActive))
  3134.     {
  3135.     client->errorValue = stuff->mode;
  3136.         return BadValue;
  3137.     }
  3138.     SaveScreens(SCREEN_SAVER_FORCER, (int)stuff->mode);
  3139.     return client->noClientException;
  3140. }
  3141.  
  3142. int ProcNoOperation(client)
  3143.     register ClientPtr client;
  3144. {
  3145.     REQUEST(xReq);
  3146.  
  3147.     REQUEST_AT_LEAST_SIZE(xReq);
  3148.     
  3149.     /* noop -- don't do anything */
  3150.     return(client->noClientException);
  3151. }
  3152.  
  3153. void
  3154. InitProcVectors()
  3155. {
  3156.     int i;
  3157.     for (i = 0; i<256; i++)
  3158.     {
  3159.     if(!ProcVector[i])
  3160.     {
  3161.             ProcVector[i] = SwappedProcVector[i] = ProcBadRequest;
  3162.         ReplySwapVector[i] = NotImplemented;
  3163.     }
  3164.     }
  3165.     for(i = LASTEvent; i < 128; i++)
  3166.     {
  3167.     EventSwapVector[i] = NotImplemented;
  3168.     }
  3169.     
  3170. }
  3171.  
  3172. /**********************
  3173.  * CloseDownClient
  3174.  *
  3175.  *  Client can either mark his resources destroy or retain.  If retained and
  3176.  *  then killed again, the client is really destroyed.
  3177.  *********************/
  3178.  
  3179. void
  3180. CloseDownClient(client)
  3181.     register ClientPtr client;
  3182. {
  3183.     if (!client->clientGone)
  3184.     {
  3185.     /* ungrab server if grabbing client dies */
  3186.     if (grabbingClient &&  (onlyClient == client))
  3187.     {
  3188.         grabbingClient = FALSE;
  3189.         ListenToAllClients();
  3190.     }
  3191.     DeleteClientFromAnySelections(client);
  3192.     ReleaseActiveGrabs(client);
  3193.     
  3194.     if (client->closeDownMode == DestroyAll)
  3195.     {
  3196.         client->clientGone = TRUE;  /* so events aren't sent to client */
  3197.         CloseDownConnection(client);
  3198.         FreeClientResources(client);
  3199.         if (client->index < nextFreeClientID)
  3200.         nextFreeClientID = client->index;
  3201.         clients[client->index] = NullClient;
  3202.         if ((client->requestVector != InitialVector) &&
  3203.         (--nClients == 0))
  3204. #ifdef GPROF
  3205.         dispatchException |= DE_TERMINATE;
  3206. #else
  3207.         dispatchException |= DE_RESET;
  3208. #endif
  3209.         xfree(client);
  3210.     }
  3211.     else
  3212.     {
  3213.         client->clientGone = TRUE;
  3214.         CloseDownConnection(client);
  3215.         --nClients;
  3216.     }
  3217.     }
  3218.     else
  3219.     {
  3220.     /* really kill resources this time */
  3221.         FreeClientResources(client);
  3222.     if (client->index < nextFreeClientID)
  3223.         nextFreeClientID = client->index;
  3224.     clients[client->index] = NullClient;
  3225.         xfree(client);
  3226.     }
  3227.  
  3228.     while (!clients[currentMaxClients-1])
  3229.       currentMaxClients--;
  3230. }
  3231.  
  3232. static void
  3233. KillAllClients()
  3234. {
  3235.     int i;
  3236.     for (i=1; i<currentMaxClients; i++)
  3237.         if (clients[i])
  3238.             CloseDownClient(clients[i]);     
  3239. }
  3240.  
  3241. /*********************
  3242.  * CloseDownRetainedResources
  3243.  *
  3244.  *    Find all clients that are gone and have terminated in RetainTemporary 
  3245.  *    and  destroy their resources.
  3246.  *********************/
  3247.  
  3248. CloseDownRetainedResources()
  3249. {
  3250.     register int i;
  3251.     register ClientPtr client;
  3252.  
  3253.     for (i=1; i<currentMaxClients; i++)
  3254.     {
  3255.         client = clients[i];
  3256.         if (client && (client->closeDownMode == RetainTemporary)
  3257.         && (client->clientGone))
  3258.         CloseDownClient(client);
  3259.     }
  3260. }
  3261.  
  3262. /************************
  3263.  * int NextAvailableClient(ospriv)
  3264.  *
  3265.  * OS dependent portion can't assign client id's because of CloseDownModes.
  3266.  * Returns NULL if there are no free clients.
  3267.  *************************/
  3268.  
  3269. ClientPtr
  3270. NextAvailableClient(ospriv)
  3271.     pointer ospriv;
  3272. {
  3273.     register int i;
  3274.     register ClientPtr client;
  3275.     xReq data;
  3276.  
  3277.     i = nextFreeClientID;
  3278.     if (i == MAXCLIENTS)
  3279.     return (ClientPtr)NULL;
  3280.     clients[i] = client = (ClientPtr)xalloc(sizeof(ClientRec));
  3281.     if (!client)
  3282.     return (ClientPtr)NULL;
  3283.     client->index = i;
  3284.     client->sequence = 0; 
  3285.     client->clientAsMask = ((Mask)i) << CLIENTOFFSET;
  3286.     client->closeDownMode = DestroyAll;
  3287.     client->clientGone = FALSE;
  3288.     client->lastDrawable = (DrawablePtr) NULL;
  3289.     client->lastDrawableID = INVALID;
  3290.     client->lastGC = (GCPtr) NULL;
  3291.     client->lastGCID = INVALID;
  3292.     client->numSaved = 0;
  3293.     client->saveSet = (pointer *)NULL;
  3294.     client->noClientException = Success;
  3295. #ifdef DEBUG
  3296.     client->requestLogIndex = 0;
  3297. #endif
  3298.     client->requestVector = InitialVector;
  3299.     client->osPrivate = ospriv;
  3300.     client->swapped = FALSE;
  3301.     if (!InitClientResources(client))
  3302.     {
  3303.     xfree(client);
  3304.     return (ClientPtr)NULL;
  3305.     }
  3306.     data.reqType = 1;
  3307.     data.length = (sz_xReq + sz_xConnClientPrefix) >> 2;
  3308.     if (!InsertFakeRequest(client, (char *)&data, sz_xReq))
  3309.     {
  3310.     FreeClientResources(client);
  3311.     xfree(client);
  3312.     return (ClientPtr)NULL;
  3313.     }
  3314.     if (i == currentMaxClients)
  3315.     currentMaxClients++;
  3316.     while ((nextFreeClientID < MAXCLIENTS) && clients[nextFreeClientID])
  3317.     nextFreeClientID++;
  3318.     return(client);
  3319. }
  3320.  
  3321. int
  3322. ProcInitialConnection(client)
  3323.     register ClientPtr client;
  3324. {
  3325.     REQUEST(xReq);
  3326.     register xConnClientPrefix *prefix;
  3327.     int whichbyte = 1;
  3328.  
  3329.     prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3330.     if ((prefix->byteOrder != 'l') && (prefix->byteOrder != 'B'))
  3331.     return (client->noClientException = -1);
  3332.     if (((*(char *) &whichbyte) && (prefix->byteOrder == 'B')) ||
  3333.     (!(*(char *) &whichbyte) && (prefix->byteOrder == 'l')))
  3334.     {
  3335.     client->swapped = TRUE;
  3336.     SwapConnClientPrefix(prefix);
  3337.     }
  3338.     stuff->reqType = 2;
  3339.     stuff->length += ((prefix->nbytesAuthProto + 3) >> 2) +
  3340.              ((prefix->nbytesAuthString + 3) >> 2);
  3341.     if (client->swapped)
  3342.     {
  3343.     swaps(&stuff->length, whichbyte);
  3344.     }
  3345.     ResetCurrentRequest(client);
  3346.     return (client->noClientException);
  3347. }
  3348.  
  3349. int
  3350. ProcEstablishConnection(client)
  3351.     register ClientPtr client;
  3352. {
  3353.     char *reason, *auth_proto, *auth_string;
  3354.     register xConnClientPrefix *prefix;
  3355.     register xWindowRoot *root;
  3356.     register int i;
  3357.     REQUEST(xReq);
  3358.  
  3359.     prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3360.     auth_proto = (char *)prefix + sz_xConnClientPrefix;
  3361.     auth_string = auth_proto + ((prefix->nbytesAuthProto + 3) & ~3);
  3362.     if ((prefix->majorVersion != X_PROTOCOL) ||
  3363.     (prefix->minorVersion != X_PROTOCOL_REVISION))
  3364.     reason = "Protocol version mismatch";
  3365.     else
  3366.     reason = ClientAuthorized(client,
  3367.                   (unsigned short)prefix->nbytesAuthProto,
  3368.                   auth_proto,
  3369.                   (unsigned short)prefix->nbytesAuthString,
  3370.                   auth_string);
  3371.     if (reason)
  3372.     {
  3373.     xConnSetupPrefix csp;
  3374.     char pad[3];
  3375.  
  3376.     csp.success = xFalse;
  3377.     csp.lengthReason = strlen(reason);
  3378.     csp.length = (csp.lengthReason + 3) >> 2;
  3379.     csp.majorVersion = X_PROTOCOL;
  3380.     csp.minorVersion = X_PROTOCOL_REVISION;
  3381.     if (client->swapped)
  3382.         WriteSConnSetupPrefix(client, &csp);
  3383.     else
  3384.         (void)WriteToClient(client, sz_xConnSetupPrefix, (char *) &csp);
  3385.         (void)WriteToClient(client, (int)csp.lengthReason, reason);
  3386.     if (csp.lengthReason & 3)
  3387.         (void)WriteToClient(client, (int)(4 - (csp.lengthReason & 3)),
  3388.                 pad);
  3389.     return (client->noClientException = -1);
  3390.     }
  3391.  
  3392.     nClients++;
  3393.     client->requestVector = client->swapped ? SwappedProcVector : ProcVector;
  3394.     client->sequence = 0;
  3395.     ((xConnSetup *)ConnectionInfo)->ridBase = client->clientAsMask;
  3396.     ((xConnSetup *)ConnectionInfo)->ridMask = 0xfffff;
  3397.     /* fill in the "currentInputMask" */
  3398.     root = (xWindowRoot *)(ConnectionInfo + connBlockScreenStart);
  3399.     for (i=0; i<screenInfo.numScreens; i++) 
  3400.     {
  3401.     register int j;
  3402.     register xDepth *pDepth;
  3403.  
  3404.         root->currentInputMask = WindowTable[i]->eventMask |
  3405.                      wOtherEventMasks (WindowTable[i]);
  3406.     pDepth = (xDepth *)(root + 1);
  3407.     for (j = 0; j < root->nDepths; j++)
  3408.     {
  3409.         pDepth = (xDepth *)(((char *)(pDepth + 1)) +
  3410.                 pDepth->nVisuals * sizeof(xVisualType));
  3411.     }
  3412.     root = (xWindowRoot *)pDepth;
  3413.     }
  3414.  
  3415.     if (client->swapped)
  3416.     {
  3417.     WriteSConnSetupPrefix(client, &connSetupPrefix);
  3418.     WriteSConnectionInfo(client,
  3419.                  (unsigned long)(connSetupPrefix.length << 2),
  3420.                  ConnectionInfo);
  3421.     }
  3422.     else
  3423.     {
  3424.     (void)WriteToClient(client, sizeof(xConnSetupPrefix),
  3425.                 (char *) &connSetupPrefix);
  3426.     (void)WriteToClient(client, (int)(connSetupPrefix.length << 2),
  3427.                 ConnectionInfo);
  3428.     }
  3429.     return (client->noClientException);
  3430. }
  3431.  
  3432. SendErrorToClient(client, majorCode, minorCode, resId, errorCode)
  3433.     ClientPtr client;
  3434.     unsigned majorCode;
  3435.     unsigned short minorCode;
  3436.     XID resId;
  3437.     int errorCode;
  3438. {
  3439.     xError rep;
  3440.  
  3441.     rep.type = X_Error;
  3442.     rep.sequenceNumber = client->sequence;
  3443.     rep.errorCode = errorCode;
  3444.     rep.majorCode = majorCode;
  3445.     rep.minorCode = minorCode;
  3446.     rep.resourceID = resId;
  3447.  
  3448.     WriteEventsToClient (client, 1, (xEvent *)&rep);
  3449. }
  3450.  
  3451. void
  3452. DeleteWindowFromAnySelections(pWin)
  3453.     WindowPtr pWin;
  3454. {
  3455.     register int i;
  3456.  
  3457.     for (i = 0; i< NumCurrentSelections; i++)
  3458.         if (CurrentSelections[i].pWin == pWin)
  3459.         {
  3460.             CurrentSelections[i].pWin = (WindowPtr)NULL;
  3461.             CurrentSelections[i].window = None;
  3462.         CurrentSelections[i].client = NullClient;
  3463.     }
  3464. }
  3465.  
  3466. static void
  3467. DeleteClientFromAnySelections(client)
  3468.     ClientPtr client;
  3469. {
  3470.     register int i;
  3471.  
  3472.     for (i = 0; i< NumCurrentSelections; i++)
  3473.         if (CurrentSelections[i].client == client)
  3474.         {
  3475.             CurrentSelections[i].pWin = (WindowPtr)NULL;
  3476.             CurrentSelections[i].window = None;
  3477.         CurrentSelections[i].client = NullClient;
  3478.     }
  3479. }
  3480.  
  3481. void
  3482. MarkClientException(client)
  3483.     ClientPtr client;
  3484. {
  3485.     client->noClientException = -1;
  3486. }
  3487.